summaryrefslogtreecommitdiff
path: root/linux-core
diff options
context:
space:
mode:
authorEric Anholt <eric@anholt.net>2008-06-10 15:30:23 -0700
committerEric Anholt <eric@anholt.net>2008-06-10 22:57:07 -0700
commit2150da5d1a57d25d0f4bc39bb6c883d410f586d1 (patch)
treed66c8062573491fb5caab64623e319e899262935 /linux-core
parent500c81d194115fb3c4b97d742519689478eeb4e8 (diff)
[gem] Manage the ringbuffer from the kernel in the GEM case.
This requires that the X Server use the execbuf interface for buffer submission, as it no longer has direct access to the ring. This is therefore a flag day for the gem interface. This also adds enter/leavevt ioctls for use by the X Server. These would get stubbed out in a modesetting implementation, but are required while in an environment where the device's state is only managed by the DRM while X has the VT.
Diffstat (limited to 'linux-core')
-rw-r--r--linux-core/drmP.h3
-rw-r--r--linux-core/drm_gem.c3
-rw-r--r--linux-core/i915_gem.c242
3 files changed, 229 insertions, 19 deletions
diff --git a/linux-core/drmP.h b/linux-core/drmP.h
index 8246f44a..6a7f28d1 100644
--- a/linux-core/drmP.h
+++ b/linux-core/drmP.h
@@ -1371,6 +1371,9 @@ drm_gem_init (struct drm_device *dev);
void
drm_gem_object_free (struct kref *kref);
+struct drm_gem_object *
+drm_gem_object_alloc(struct drm_device *dev, size_t size);
+
void
drm_gem_object_handle_free (struct kref *kref);
diff --git a/linux-core/drm_gem.c b/linux-core/drm_gem.c
index b726e598..a8ecaf76 100644
--- a/linux-core/drm_gem.c
+++ b/linux-core/drm_gem.c
@@ -80,7 +80,7 @@ drm_gem_init(struct drm_device *dev)
/**
* Allocate a GEM object of the specified size with shmfs backing store
*/
-static struct drm_gem_object *
+struct drm_gem_object *
drm_gem_object_alloc(struct drm_device *dev, size_t size)
{
struct drm_gem_object *obj;
@@ -117,6 +117,7 @@ drm_gem_object_alloc(struct drm_device *dev, size_t size)
atomic_inc(&dev->object_count);
return obj;
}
+EXPORT_SYMBOL(drm_gem_object_alloc);
/**
* Removes the mapping from handle to filp for this object.
diff --git a/linux-core/i915_gem.c b/linux-core/i915_gem.c
index d60a98f8..2564f418 100644
--- a/linux-core/i915_gem.c
+++ b/linux-core/i915_gem.c
@@ -702,14 +702,17 @@ i915_gem_object_get_page_list(struct drm_gem_object *obj)
BUG_ON(obj_priv->page_list != NULL);
obj_priv->page_list = drm_calloc(page_count, sizeof(struct page *),
DRM_MEM_DRIVER);
- if (obj_priv->page_list == NULL)
+ if (obj_priv->page_list == NULL) {
+ DRM_ERROR("Faled to allocate page list\n");
return -ENOMEM;
+ }
for (i = 0; i < page_count; i++) {
obj_priv->page_list[i] =
find_or_create_page(obj->filp->f_mapping, i, GFP_HIGHUSER);
if (obj_priv->page_list[i] == NULL) {
+ DRM_ERROR("Failed to find_or_create_page()\n");
i915_gem_object_free_page_list(obj);
return -ENOMEM;
}
@@ -758,14 +761,17 @@ i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
DRM_INFO("%s: GTT full, evicting something\n", __func__);
#endif
if (list_empty(&dev_priv->mm.inactive_list) &&
+ list_empty(&dev_priv->mm.flushing_list) &&
list_empty(&dev_priv->mm.active_list)) {
DRM_ERROR("GTT full, but LRU list empty\n");
return -ENOMEM;
}
ret = i915_gem_evict_something(dev);
- if (ret != 0)
+ if (ret != 0) {
+ DRM_ERROR("Failed to evict a buffer\n");
return ret;
+ }
goto search_free;
}
@@ -1383,6 +1389,7 @@ int
i915_gem_execbuffer(struct drm_device *dev, void *data,
struct drm_file *file_priv)
{
+ drm_i915_private_t *dev_priv = dev->dev_private;
struct drm_i915_gem_execbuffer *args = data;
struct drm_i915_gem_exec_object *exec_list = NULL;
struct drm_gem_object **object_list = NULL;
@@ -1423,6 +1430,12 @@ i915_gem_execbuffer(struct drm_device *dev, void *data,
mutex_lock(&dev->struct_mutex);
+ if (dev_priv->mm.suspended) {
+ DRM_ERROR("Execbuf while VT-switched.\n");
+ mutex_unlock(&dev->struct_mutex);
+ return -EBUSY;
+ }
+
/* Zero the gloabl flush/invalidate flags. These
* will be modified as each object is bound to the
* gtt
@@ -1560,6 +1573,37 @@ pre_mutex_err:
}
int
+i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
+{
+ struct drm_device *dev = obj->dev;
+ struct drm_i915_gem_object *obj_priv = obj->driver_private;
+ int ret;
+
+ if (obj_priv->gtt_space == NULL) {
+ ret = i915_gem_object_bind_to_gtt(obj, alignment);
+ if (ret != 0) {
+ DRM_ERROR("Failure to bind in "
+ "i915_gem_pin_ioctl(): %d\n",
+ ret);
+ drm_gem_object_unreference(obj);
+ mutex_unlock(&dev->struct_mutex);
+ return ret;
+ }
+ }
+
+ obj_priv->pin_count++;
+ return 0;
+}
+
+void
+i915_gem_object_unpin(struct drm_gem_object *obj)
+{
+ struct drm_i915_gem_object *obj_priv = obj->driver_private;
+
+ obj_priv->pin_count--;
+}
+
+int
i915_gem_pin_ioctl(struct drm_device *dev, void *data,
struct drm_file *file_priv)
{
@@ -1578,22 +1622,15 @@ i915_gem_pin_ioctl(struct drm_device *dev, void *data,
mutex_unlock(&dev->struct_mutex);
return -EINVAL;
}
-
obj_priv = obj->driver_private;
- if (obj_priv->gtt_space == NULL) {
- ret = i915_gem_object_bind_to_gtt(obj,
- (unsigned) args->alignment);
- if (ret != 0) {
- DRM_ERROR("Failure to bind in "
- "i915_gem_pin_ioctl(): %d\n",
- ret);
- drm_gem_object_unreference(obj);
- mutex_unlock(&dev->struct_mutex);
- return ret;
- }
+
+ ret = i915_gem_object_pin(obj, args->alignment);
+ if (ret != 0) {
+ drm_gem_object_unreference(obj);
+ mutex_unlock(&dev->struct_mutex);
+ return ret;
}
- obj_priv->pin_count++;
args->offset = obj_priv->gtt_offset;
drm_gem_object_unreference(obj);
mutex_unlock(&dev->struct_mutex);
@@ -1607,7 +1644,6 @@ i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
{
struct drm_i915_gem_pin *args = data;
struct drm_gem_object *obj;
- struct drm_i915_gem_object *obj_priv;
mutex_lock(&dev->struct_mutex);
@@ -1620,8 +1656,8 @@ i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
return -EINVAL;
}
- obj_priv = obj->driver_private;
- obj_priv->pin_count--;
+ i915_gem_object_unpin(obj);
+
drm_gem_object_unreference(obj);
mutex_unlock(&dev->struct_mutex);
return 0;
@@ -1757,3 +1793,173 @@ i915_gem_lastclose(struct drm_device *dev)
mutex_unlock(&dev->struct_mutex);
}
+
+static int
+i915_gem_init_ringbuffer(struct drm_device *dev)
+{
+ drm_i915_private_t *dev_priv = dev->dev_private;
+ struct drm_gem_object *obj;
+ struct drm_i915_gem_object *obj_priv;
+ int ret;
+
+ obj = drm_gem_object_alloc(dev, 128 * 1024);
+ if (obj == NULL) {
+ DRM_ERROR("Failed to allocate ringbuffer\n");
+ return -ENOMEM;
+ }
+ obj_priv = obj->driver_private;
+
+ ret = i915_gem_object_pin(obj, 4096);
+ if (ret != 0)
+ return ret;
+
+ /* Set up the kernel mapping for the ring. */
+ dev_priv->ring.Size = obj->size;
+ dev_priv->ring.tail_mask = obj->size - 1;
+
+ dev_priv->ring.map.offset = dev->agp->base + obj_priv->gtt_offset;
+ dev_priv->ring.map.size = obj->size;
+ dev_priv->ring.map.type = 0;
+ dev_priv->ring.map.flags = 0;
+ dev_priv->ring.map.mtrr = 0;
+
+ drm_core_ioremap(&dev_priv->ring.map, dev);
+ if (dev_priv->ring.map.handle == NULL) {
+ DRM_ERROR("Failed to map ringbuffer.\n");
+ memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
+ drm_gem_object_unreference(obj);
+ return -EINVAL;
+ }
+ dev_priv->ring.ring_obj = obj;
+ dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
+
+ /* Stop the ring if it's running. */
+ I915_WRITE(LP_RING + RING_LEN, 0);
+ I915_WRITE(LP_RING + RING_HEAD, 0);
+ I915_WRITE(LP_RING + RING_TAIL, 0);
+ I915_WRITE(LP_RING + RING_START, 0);
+
+ /* Initialize the ring. */
+ I915_WRITE(LP_RING + RING_START, obj_priv->gtt_offset);
+ I915_WRITE(LP_RING + RING_LEN,
+ ((obj->size - 4096) & RING_NR_PAGES) |
+ RING_NO_REPORT |
+ RING_VALID);
+
+ /* Update our cache of the ring state */
+ i915_kernel_lost_context(dev);
+
+ return 0;
+}
+
+static void
+i915_gem_cleanup_ringbuffer(struct drm_device *dev)
+{
+ drm_i915_private_t *dev_priv = dev->dev_private;
+
+ if (dev_priv->ring.ring_obj == NULL)
+ return;
+
+ drm_core_ioremapfree(&dev_priv->ring.map, dev);
+
+ i915_gem_object_unpin(dev_priv->ring.ring_obj);
+ drm_gem_object_unreference(dev_priv->ring.ring_obj);
+
+ memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
+}
+
+int
+i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
+ struct drm_file *file_priv)
+{
+ drm_i915_private_t *dev_priv = dev->dev_private;
+ int ret;
+
+ ret = i915_gem_init_ringbuffer(dev);
+ if (ret != 0)
+ return ret;
+
+ mutex_lock(&dev->struct_mutex);
+ dev_priv->mm.suspended = 0;
+ mutex_unlock(&dev->struct_mutex);
+ return 0;
+}
+
+/** Unbinds all objects that are on the given buffer list. */
+static int
+i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
+{
+ struct drm_gem_object *obj;
+ struct drm_i915_gem_object *obj_priv;
+ int ret;
+
+ while (!list_empty(head)) {
+ obj_priv = list_first_entry(head,
+ struct drm_i915_gem_object,
+ list);
+ obj = obj_priv->obj;
+
+ if (obj_priv->pin_count != 0) {
+ DRM_ERROR("Pinned object in unbind list\n");
+ mutex_unlock(&dev->struct_mutex);
+ return -EINVAL;
+ }
+
+ ret = i915_gem_object_unbind(obj);
+ if (ret != 0) {
+ DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
+ ret);
+ mutex_unlock(&dev->struct_mutex);
+ return ret;
+ }
+ }
+
+
+ return 0;
+}
+
+int
+i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
+ struct drm_file *file_priv)
+{
+ drm_i915_private_t *dev_priv = dev->dev_private;
+
+ mutex_lock(&dev->struct_mutex);
+ /* Hack! Don't let anybody do execbuf while we don't control the chip.
+ * We need to replace this with a semaphore, or something.
+ */
+ dev_priv->mm.suspended = 1;
+
+ /* Move all buffers out of the GTT. */
+ i915_gem_evict_from_list(dev, &dev_priv->mm.active_list);
+ i915_gem_evict_from_list(dev, &dev_priv->mm.flushing_list);
+ i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
+
+ /* Make sure the harware's idle. */
+ while (!list_empty(&dev_priv->mm.request_list)) {
+ struct drm_i915_gem_request *request;
+ int ret;
+
+ request = list_first_entry(&dev_priv->mm.request_list,
+ struct drm_i915_gem_request,
+ list);
+
+ ret = i915_wait_request(dev, request->seqno);
+ if (ret != 0) {
+ DRM_ERROR("Error waiting for idle at LeaveVT: %d\n",
+ ret);
+ mutex_unlock(&dev->struct_mutex);
+ return ret;
+ }
+ }
+
+ BUG_ON(!list_empty(&dev_priv->mm.active_list));
+ BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
+ BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
+
+ i915_gem_cleanup_ringbuffer(dev);
+
+ mutex_unlock(&dev->struct_mutex);
+
+ return 0;
+}
d='n678' href='#n678'>678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
/*
 * Copyright (C) 2008 Maarten Maathuis.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include "nv50_kms_wrapper.h"
#include "drm_crtc_helper.h" /* be careful what you use from this */

/* This file serves as the interface between the common kernel modesetting code and the device dependent implementation. */

/*
 * Get private functions.
 */

struct nv50_kms_priv *nv50_get_kms_priv(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	return dev_priv->kms_priv;
}

/*
 * Allocation functions.
 */

static void *nv50_kms_alloc_crtc(struct drm_device *dev)
{
	struct nv50_kms_priv *kms_priv = nv50_get_kms_priv(dev);
	struct nv50_kms_crtc *crtc = kzalloc(sizeof(struct nv50_kms_crtc), GFP_KERNEL);

	if (!crtc)
		return NULL;

	list_add_tail(&crtc->item, &kms_priv->crtcs);

	return &(crtc->priv);
}

static void *nv50_kms_alloc_output(struct drm_device *dev)
{
	struct nv50_kms_priv *kms_priv = nv50_get_kms_priv(dev);
	struct nv50_kms_encoder *encoder = kzalloc(sizeof(struct nv50_kms_encoder), GFP_KERNEL);

	if (!encoder)
		return NULL;

	list_add_tail(&encoder->item, &kms_priv->encoders);

	return &(encoder->priv);
}

static void *nv50_kms_alloc_connector(struct drm_device *dev)
{
	struct nv50_kms_priv *kms_priv = nv50_get_kms_priv(dev);
	struct nv50_kms_connector *connector = kzalloc(sizeof(struct nv50_kms_connector), GFP_KERNEL);

	if (!connector)
		return NULL;

	list_add_tail(&connector->item, &kms_priv->connectors);

	return &(connector->priv);
}

static void nv50_kms_free_crtc(void *crtc)
{
	struct nv50_kms_crtc *kms_crtc = from_nv50_crtc(crtc);

	list_del(&kms_crtc->item);

	kfree(kms_crtc);
}

static void nv50_kms_free_output(void *output)
{
	struct nv50_kms_encoder *kms_encoder = from_nv50_output(output);

	list_del(&kms_encoder->item);

	kfree(kms_encoder);
}

static void nv50_kms_free_connector(void *connector)
{
	struct nv50_kms_connector *kms_connector = from_nv50_connector(connector);

	list_del(&kms_connector->item);

	kfree(kms_connector);
}

/*
 * Mode conversion functions.
 */

static struct nouveau_hw_mode *nv50_kms_to_hw_mode(struct drm_display_mode *mode)
{
	struct nouveau_hw_mode *hw_mode = kzalloc(sizeof(struct nouveau_hw_mode), GFP_KERNEL);
	if (!hw_mode)
		return NULL;

	/* create hw values. */
	hw_mode->clock = mode->clock;
	hw_mode->flags = hw_mode->flags;

	hw_mode->hdisplay = mode->hdisplay;
	hw_mode->hsync_start = mode->hsync_start;
	hw_mode->hsync_end = mode->hsync_end;
	hw_mode->htotal = mode->htotal;

	hw_mode->hblank_start = mode->hdisplay + 1;
	hw_mode->hblank_end = mode->htotal;

	hw_mode->vdisplay = mode->vdisplay;
	hw_mode->vsync_start = mode->vsync_start;
	hw_mode->vsync_end = mode->vsync_end;
	hw_mode->vtotal = mode->vtotal;

	hw_mode->vblank_start = mode->vdisplay + 1;
	hw_mode->vblank_end = mode->vtotal;

	return hw_mode;
}

/*
 * State mirroring functions.
 */

static void nv50_kms_mirror_routing(struct drm_device *dev)
{
	struct nv50_display *display = nv50_get_display(dev);
	struct nv50_crtc *crtc = NULL;
	struct nv50_output *output = NULL;
	struct nv50_connector *connector = NULL;
	struct drm_connector *drm_connector = NULL;
	struct drm_crtc *drm_crtc = NULL;

	/* Wipe all previous connections. */
	list_for_each_entry(connector, &display->connectors, item) {
		connector->output = NULL;
	}

	list_for_each_entry(output, &display->outputs, item) {
		output->crtc = NULL;
	}

	list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
		if (drm_connector->encoder) {
			output = to_nv50_output(drm_connector->encoder);
			connector = to_nv50_connector(drm_connector);

			/* hook up output to connector. */
			connector->output = output;

			if (drm_connector->encoder->crtc) {
				crtc = to_nv50_crtc(drm_connector->encoder->crtc);

				/* hook up output to crtc. */
				output->crtc = crtc;
			}
		}
	}

	/* mirror crtc active state */
	list_for_each_entry(drm_crtc, &dev->mode_config.crtc_list, head) {
		crtc = to_nv50_crtc(drm_crtc);

		crtc->enabled = drm_crtc->enabled;
	}
}

/*
 * FB functions.
 */

static void nv50_kms_framebuffer_destroy(struct drm_framebuffer *drm_framebuffer)
{
	drm_framebuffer_cleanup(drm_framebuffer);

	kfree(drm_framebuffer);
}

static const struct drm_framebuffer_funcs nv50_kms_fb_funcs = {
	.destroy = nv50_kms_framebuffer_destroy,
};

/*
 * Mode config functions.
 */

static struct drm_framebuffer *nv50_kms_framebuffer_create(struct drm_device *dev,
						struct drm_file *file_priv, struct drm_mode_fb_cmd *mode_cmd)
{
	struct drm_framebuffer *drm_framebuffer = kzalloc(sizeof(struct drm_framebuffer), GFP_KERNEL);
	if (!drm_framebuffer)
		return NULL;

	drm_framebuffer_init(dev, drm_framebuffer, &nv50_kms_fb_funcs);
	drm_helper_mode_fill_fb_struct(drm_framebuffer, mode_cmd);

	return drm_framebuffer;
}

static int nv50_kms_fb_changed(struct drm_device *dev)
{
	return 0; /* not needed until nouveaufb? */
}

static const struct drm_mode_config_funcs nv50_kms_mode_funcs = {
	.resize_fb = NULL,
	.fb_create = nv50_kms_framebuffer_create,
	.fb_changed = nv50_kms_fb_changed,
};

/*
 * CRTC functions.
 */

static int nv50_kms_crtc_cursor_set(struct drm_crtc *drm_crtc, 
				    struct drm_file *file_priv,
				    uint32_t buffer_handle,
				    uint32_t width, uint32_t height)
{
	struct nv50_crtc *crtc = to_nv50_crtc(drm_crtc);
	struct nv50_display *display = nv50_get_display(crtc->dev);
	int rval = 0;

	if (width != 64 || height != 64)
		return -EINVAL;

	/* set bo before doing show cursor */
	if (buffer_handle) {
		rval = crtc->cursor->set_bo(crtc, (drm_handle_t) buffer_handle);
		if (rval != 0)
			goto out;
	}

	crtc->cursor->visible = buffer_handle ? true : false;

	if (buffer_handle) {
		rval = crtc->cursor->show(crtc);
		if (rval != 0)
			goto out;
	} else { /* no handle implies hiding the cursor */
		rval = crtc->cursor->hide(crtc);
		goto out;
	}

	if (rval != 0)
		return rval;

out:
	/* in case this triggers any other cursor changes */
	display->update(display);

	return rval;
}

static int nv50_kms_crtc_cursor_move(struct drm_crtc *drm_crtc, int x, int y)
{
	struct nv50_crtc *crtc = to_nv50_crtc(drm_crtc);

	return crtc->cursor->set_pos(crtc, x, y);
}

void nv50_kms_crtc_gamma_set(struct drm_crtc *drm_crtc, u16 *r, u16 *g, u16 *b,
		uint32_t size)
{
	struct nv50_crtc *crtc = to_nv50_crtc(drm_crtc);

	if (size != 256)
		return;

	crtc->lut->set(crtc, (uint16_t *)r, (uint16_t *)g, (uint16_t *)b);
}

int nv50_kms_crtc_set_config(struct drm_mode_set *set)
{
	int rval = 0, i;
	uint32_t crtc_mask = 0;
	struct drm_device *dev = NULL;
	struct drm_nouveau_private *dev_priv = NULL;
	struct nv50_display *display = NULL;
	struct drm_connector *drm_connector = NULL;
	struct drm_encoder *drm_encoder = NULL;
	struct drm_crtc *drm_crtc = NULL;

	struct nv50_crtc *crtc = NULL;
	struct nv50_output *output = NULL;
	struct nv50_connector *connector = NULL;
	struct nouveau_hw_mode *hw_mode = NULL;
	struct nv50_fb_info fb_info;

	bool blank = false;
	bool switch_fb = false;
	bool modeset = false;

	NV50_DEBUG("\n");

	/*
	 * Supported operations:
	 * - Switch mode.
	 * - Switch framebuffer.
	 * - Blank screen.
	 */

	/* Sanity checking */
	if (!set) {
		DRM_ERROR("Sanity check failed\n");
		goto out;
	}

	if (!set->crtc) {
		DRM_ERROR("Sanity check failed\n");
		goto out;
	}

	if (set->mode) {
		if (set->fb) {
			if (!drm_mode_equal(set->mode, &set->crtc->mode))
				modeset = true;

			if (set->fb != set->crtc->fb)
				switch_fb = true;

			if (set->x != set->crtc->x || set->y != set->crtc->y)
				switch_fb = true;
		}
	} else {
		blank = true;
	}

	if (!set->connectors && !blank) {
		DRM_ERROR("Sanity check failed\n");
		goto out;
	}

	/* Basic variable setting */
	dev = set->crtc->dev;
	dev_priv = dev->dev_private;
	display = nv50_get_display(dev);
	crtc = to_nv50_crtc(set->crtc);

	/**
	 * Wiring up the encoders and connectors.
	 */

	/* for switch_fb we verify if any important changes happened */
	if (!blank) {
		/* Mode validation */
		hw_mode = nv50_kms_to_hw_mode(set->mode);

		rval = crtc->validate_mode(crtc, hw_mode);

		if (rval != MODE_OK) {
			DRM_ERROR("Mode not ok\n");
			goto out;
		}

		for (i = 0; i < set->num_connectors; i++) {
			drm_connector = set->connectors[i];
			if (!drm_connector) {
				DRM_ERROR("No connector\n");
				goto out;
			}
			connector = to_nv50_connector(drm_connector);

			/* This is to ensure it knows the connector subtype. */
			drm_connector->funcs->fill_modes(drm_connector, 0, 0);

			output = connector->to_output(connector, nv50_kms_connector_get_digital(drm_connector));
			if (!output) {
				DRM_ERROR("No output\n");
				goto out;
			}

			rval = output->validate_mode(output, hw_mode);
			if (rval != MODE_OK) {
				DRM_ERROR("Mode not ok\n");
				goto out;
			}

			/* verify if any "sneaky" changes happened */
			if (output != connector->output)
				modeset = true;

			if (output->crtc != crtc)
				modeset = true;
		}
	}

	/* Now we verified if anything changed, fail if nothing has. */
	if (!modeset && !switch_fb && !blank)
		DRM_INFO("A seemingly empty modeset encountered, this could be a bug.\n");

	/* Validation done, move on to cleaning of existing structures. */
	if (modeset) {
		/* find encoders that use this crtc. */
		list_for_each_entry(drm_encoder, &dev->mode_config.encoder_list, head) {
			if (drm_encoder->crtc == set->crtc) {
				/* find the connector that goes with it */
				list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
					if (drm_connector->encoder == drm_encoder) {
						drm_connector->encoder =  NULL;
						break;
					}
				}
				drm_encoder->crtc = NULL;
			}
		}

		/* now find if our desired encoders or connectors are in use already. */
		for (i = 0; i < set->num_connectors; i++) {
			drm_connector = set->connectors[i];
			if (!drm_connector) {
				DRM_ERROR("No connector\n");
				goto out;
			}

			if (!drm_connector->encoder)
				continue;

			drm_encoder = drm_connector->encoder;
			drm_connector->encoder = NULL;

			if (!drm_encoder->crtc)
				continue;

			drm_crtc = drm_encoder->crtc;
			drm_encoder->crtc = NULL;

			drm_crtc->enabled = false;
		}

		/* Time to wire up the public encoder, the private one will be handled later. */
		for (i = 0; i < set->num_connectors; i++) {
			drm_connector = set->connectors[i];
			if (!drm_connector) {
				DRM_ERROR("No connector\n");
				goto out;
			}

			output = connector->to_output(connector, nv50_kms_connector_get_digital(drm_connector));
			if (!output) {
				DRM_ERROR("No output\n");
				goto out;
			}

			/* find the encoder public structure that matches out output structure. */
			drm_encoder = to_nv50_kms_encoder(output);

			if (!drm_encoder) {
				DRM_ERROR("No encoder\n");
				goto out;
			}

			drm_encoder->crtc = set->crtc;
			set->crtc->enabled = true;
			drm_connector->encoder = drm_encoder;
		}
	}

	/**
	 * Disable crtc.
	 */

	if (blank) {
		crtc = to_nv50_crtc(set->crtc);

		set->crtc->enabled = false;

		/* disconnect encoders and connectors */
		for (i = 0; i < set->num_connectors; i++) {
			drm_connector = set->connectors[i];

			if (!drm_connector->encoder)
				continue;

			drm_connector->encoder->crtc = NULL;
			drm_connector->encoder = NULL;
		}
	}

	/**
	 * All state should now be updated, now onto the real work.
	 */

	/* mirror everything to the private structs */
	nv50_kms_mirror_routing(dev);

	/**
	 * Bind framebuffer.
	 */

	if (switch_fb) {
		crtc = to_nv50_crtc(set->crtc);

		/* set framebuffer */
		set->crtc->fb = set->fb;

		/* set private framebuffer */
		crtc = to_nv50_crtc(set->crtc);
		fb_info.block = find_block_by_handle(dev_priv->fb_heap, set->fb->mm_handle);
		fb_info.width = set->fb->width;
		fb_info.height = set->fb->height;
		fb_info.depth = set->fb->depth;
		fb_info.bpp = set->fb->bits_per_pixel;
		fb_info.pitch = set->fb->pitch;
		fb_info.x = set->x;
		fb_info.y = set->y;

		rval = crtc->fb->bind(crtc, &fb_info);
		if (rval != 0) {
			DRM_ERROR("fb_bind failed\n");
			goto out;
		}
	}

	/* this is !cursor_show */
	if (!crtc->cursor->enabled) {
		rval = crtc->cursor->enable(crtc);
		if (rval != 0) {
			DRM_ERROR("cursor_enable failed\n");
			goto out;
		}
	}

	/**
	 * Blanking.
	 */

	if (blank) {
		crtc = to_nv50_crtc(set->crtc);

		rval = crtc->blank(crtc, true);
		if (rval != 0) {
			DRM_ERROR("blanking failed\n");
			goto out;
		}

		/* detach any outputs that are currently unused */
		list_for_each_entry(drm_encoder, &dev->mode_config.encoder_list, head) {
			if (!drm_encoder->crtc) {
				output = to_nv50_output(drm_encoder);

				rval = output->execute_mode(output, true);
				if (rval != 0) {
					DRM_ERROR("detaching output failed\n");
					goto out;
				}
			}
		}
	}

	/**
	 * Change framebuffer, without changing mode.
	 */

	if (switch_fb && !modeset && !blank) {
		crtc = to_nv50_crtc(set->crtc);

		rval = crtc->set_fb(crtc);
		if (rval != 0) {
			DRM_ERROR("set_fb failed\n");
			goto out;
		}

		/* this also sets the fb offset */
		rval = crtc->blank(crtc, false);
		if (rval != 0) {
			DRM_ERROR("unblanking failed\n");
			goto out;
		}
	}

	/**
	 * Normal modesetting.
	 */

	if (modeset) {
		crtc = to_nv50_crtc(set->crtc);

		/* disconnect unused outputs */
		list_for_each_entry(output, &display->outputs, item) {
			if (output->crtc) {
				crtc_mask |= 1 << output->crtc->index;
			} else {
				rval = output->execute_mode(output, true);
				if (rval != 0) {
					DRM_ERROR("detaching output failed\n");
					goto out;
				}
			}
		}

		/* blank any unused crtcs */
		list_for_each_entry(crtc, &display->crtcs, item) {
			if (!(crtc_mask & (1 << crtc->index)))
				crtc->blank(crtc, true);
		}

		crtc = to_nv50_crtc(set->crtc);

		rval = crtc->set_mode(crtc, hw_mode);
		if (rval != 0) {
			DRM_ERROR("crtc mode set failed\n");
			goto out;
		}

		/* find native mode. */
		list_for_each_entry(output, &display->outputs, item) {
			if (output->crtc != crtc)
				continue;

			*crtc->native_mode = *output->native_mode;
			list_for_each_entry(connector, &display->connectors, item) {
				if (connector->output != output)
					continue;

				crtc->requested_scaling_mode = connector->requested_scaling_mode;
				crtc->use_dithering = connector->use_dithering;
				break;
			}

			if (crtc->requested_scaling_mode == SCALE_NON_GPU)
				crtc->use_native_mode = false;
			else
				crtc->use_native_mode = true;

			break; /* no use in finding more than one mode */
		}

		rval = crtc->execute_mode(crtc);
		if (rval != 0) {
			DRM_ERROR("crtc execute mode failed\n");
			goto out;
		}

		list_for_each_entry(output, &display->outputs, item) {
			if (output->crtc != crtc)
				continue;

			rval = output->execute_mode(output, false);
			if (rval != 0) {
				DRM_ERROR("output execute mode failed\n");
				goto out;
			}
		}

		rval = crtc->set_scale(crtc);
		if (rval != 0) {
			DRM_ERROR("crtc set scale failed\n");
			goto out;
		}

		/* next line changes crtc, so putting it here is important */
		display->last_crtc = crtc->index;
	}

	/* always reset dpms, regardless if any other modesetting is done. */
	if (!blank) {
		/* this is executed immediately */
		list_for_each_entry(output, &display->outputs, item) {
			if (output->crtc != crtc)
				continue;

			rval = output->set_power_mode(output, DRM_MODE_DPMS_ON);
			if (rval != 0) {
				DRM_ERROR("output set power mode failed\n");
				goto out;
			}
		}

		/* update dpms state to DPMSModeOn */
		for (i = 0; i < set->num_connectors; i++) {
			drm_connector = set->connectors[i];
			if (!drm_connector) {
				DRM_ERROR("No connector\n");
				goto out;
			}

			rval = drm_connector_property_set_value(drm_connector,
					dev->mode_config.dpms_property,
					DRM_MODE_DPMS_ON);
			if (rval != 0) {
				DRM_ERROR("failed to update dpms state\n");
				goto out;
			}
		}
	}

	display->update(display);

	/* Update the current mode, now that all has gone well. */
	if (modeset) {
		set->crtc->mode = *(set->mode);
		set->crtc->x = set->x;
		set->crtc->y = set->y;
	}

	kfree(hw_mode);

	return 0;

out:
	kfree(hw_mode);

	if (rval != 0)
		return rval;
	else
		return -EINVAL;
}

static void nv50_kms_crtc_destroy(struct drm_crtc *drm_crtc)
{
	struct nv50_crtc *crtc = to_nv50_crtc(drm_crtc);

	drm_crtc_cleanup(drm_crtc);

	/* this will even destroy the public structure. */
	crtc->destroy(crtc);
}

static const struct drm_crtc_funcs nv50_kms_crtc_funcs = {
	.save = NULL,
	.restore = NULL,
	.cursor_set = nv50_kms_crtc_cursor_set,
	.cursor_move = nv50_kms_crtc_cursor_move,
	.gamma_set = nv50_kms_crtc_gamma_set,
	.set_config = nv50_kms_crtc_set_config,
	.destroy = nv50_kms_crtc_destroy,
};

static int nv50_kms_crtcs_init(struct drm_device *dev)
{
	struct nv50_display *display = nv50_get_display(dev);
	struct nv50_crtc *crtc = NULL;

	/*
	 * This may look a bit confusing, but:
	 * The internal structure is already allocated and so is the public one.
	 * Just a matter of getting to the memory and register it.
	 */
	list_for_each_entry(crtc, &display->crtcs, item) {
		struct drm_crtc *drm_crtc = to_nv50_kms_crtc(crtc);

		drm_crtc_init(dev, drm_crtc, &nv50_kms_crtc_funcs);

		/* init lut storage */
		drm_mode_crtc_set_gamma_size(drm_crtc, 256);
	}

	return 0;
}

/*
 * Encoder functions
 */

static void nv50_kms_encoder_destroy(struct drm_encoder *drm_encoder)
{
	struct nv50_output *output = to_nv50_output(drm_encoder);

	drm_encoder_cleanup(drm_encoder);

	/* this will even destroy the public structure. */
	output->destroy(output);
}

static const struct drm_encoder_funcs nv50_kms_encoder_funcs = {
	.destroy = nv50_kms_encoder_destroy,
};

static int nv50_kms_encoders_init(struct drm_device *dev)
{
	struct nv50_display *display = nv50_get_display(dev);
	struct nv50_output *output = NULL;

	list_for_each_entry(output, &display->outputs, item) {
		struct drm_encoder *drm_encoder = to_nv50_kms_encoder(output);
		uint32_t type = DRM_MODE_ENCODER_NONE;

		switch (output->type) {
			case OUTPUT_DAC:
				type = DRM_MODE_ENCODER_DAC;
				break;
			case OUTPUT_TMDS:
				type = DRM_MODE_ENCODER_TMDS;
				break;
			case OUTPUT_LVDS:
				type = DRM_MODE_ENCODER_LVDS;
				break;
			case OUTPUT_TV:
				type = DRM_MODE_ENCODER_TVDAC;
				break;
			default:
				type = DRM_MODE_ENCODER_NONE;
				break;
		}

		if (type == DRM_MODE_ENCODER_NONE) {
			DRM_ERROR("DRM_MODE_ENCODER_NONE encountered\n");
			continue;
		}

		drm_encoder_init(dev, drm_encoder, &nv50_kms_encoder_funcs, type);

		/* I've never seen possible crtc's restricted. */
		drm_encoder->possible_crtcs = 3;
		drm_encoder->possible_clones = 0;
	}

	return 0;
}

/*
 * Connector functions
 */


/* These 2 functions wrap the connector properties that deal with multiple encoders per connector. */
bool nv50_kms_connector_get_digital(struct drm_connector *drm_connector)
{
	struct drm_device *dev = drm_connector->dev;

	switch (drm_connector->connector_type) {
		case DRM_MODE_CONNECTOR_VGA:
		case DRM_MODE_CONNECTOR_SVIDEO:
			return false;
		case DRM_MODE_CONNECTOR_DVID:
		case DRM_MODE_CONNECTOR_LVDS:
			return true;
		default:
			break;
	}

	if (drm_connector->connector_type == DRM_MODE_CONNECTOR_DVII) {
		int rval;
		uint64_t prop_val;

		rval = drm_connector_property_get_value(drm_connector, dev->mode_config.dvi_i_select_subconnector_property, &prop_val);
		if (rval) {
			DRM_ERROR("Unable to find select subconnector property, defaulting to DVI-D\n");
			return true;
		}

		/* Is a subconnector explicitly selected? */
		switch (prop_val) {
			case DRM_MODE_SUBCONNECTOR_DVID:
				return true;
			case DRM_MODE_SUBCONNECTOR_DVIA:
				return false;
			default:
				break;
		}