OSDN Git Service

nv50: magic fix for "3d busted without the binary driver first" issue
[android-x86/external-libdrm.git] / libdrm / xf86drmMode.c
1 /*
2  * \file xf86drmMode.c
3  * Header for DRM modesetting interface.
4  *
5  * \author Jakob Bornecrantz <wallbraker@gmail.com>
6  *
7  * \par Acknowledgements:
8  * Feb 2007, Dave Airlie <airlied@linux.ie>
9  */
10
11 /*
12  * Copyright (c) 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
13  * Copyright (c) 2007-2008 Dave Airlie <airlied@linux.ie>
14  * Copyright (c) 2007-2008 Jakob Bornecrantz <wallbraker@gmail.com>
15  *
16  * Permission is hereby granted, free of charge, to any person obtaining a
17  * copy of this software and associated documentation files (the "Software"),
18  * to deal in the Software without restriction, including without limitation
19  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20  * and/or sell copies of the Software, and to permit persons to whom the
21  * Software is furnished to do so, subject to the following conditions:
22  *
23  * The above copyright notice and this permission notice shall be included in
24  * all copies or substantial portions of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32  * IN THE SOFTWARE.
33  *
34  */
35
36 /*
37  * TODO the types we are after are defined in diffrent headers on diffrent
38  * platforms find which headers to include to get uint32_t
39  */
40 #include <stdint.h>
41 #include <sys/ioctl.h>
42 #include <stdio.h>
43
44 #include "xf86drmMode.h"
45 #include "xf86drm.h"
46 #include <drm.h>
47 #include <string.h>
48 #include <dirent.h>
49 #include <errno.h>
50
51 #define U642VOID(x) ((void *)(unsigned long)(x))
52 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
53
54 /*
55  * Util functions
56  */
57
58 void* drmAllocCpy(void *array, int count, int entry_size)
59 {
60         char *r;
61         int i;
62
63         if (!count || !array || !entry_size)
64                 return 0;
65
66         if (!(r = drmMalloc(count*entry_size)))
67                 return 0;
68
69         for (i = 0; i < count; i++)
70                 memcpy(r+(entry_size*i), array+(entry_size*i), entry_size);
71
72         return r;
73 }
74
75 /*
76  * A couple of free functions.
77  */
78
79 void drmModeFreeModeInfo(drmModeModeInfoPtr ptr)
80 {
81         if (!ptr)
82                 return;
83
84         drmFree(ptr);
85 }
86
87 void drmModeFreeResources(drmModeResPtr ptr)
88 {
89         if (!ptr)
90                 return;
91
92         drmFree(ptr);
93
94 }
95
96 void drmModeFreeFB(drmModeFBPtr ptr)
97 {
98         if (!ptr)
99                 return;
100
101         /* we might add more frees later. */
102         drmFree(ptr);
103 }
104
105 void drmModeFreeCrtc(drmModeCrtcPtr ptr)
106 {
107         if (!ptr)
108                 return;
109
110         drmFree(ptr);
111
112 }
113
114 void drmModeFreeConnector(drmModeConnectorPtr ptr)
115 {
116         if (!ptr)
117                 return;
118
119         drmFree(ptr->modes);
120         drmFree(ptr);
121
122 }
123
124 void drmModeFreeEncoder(drmModeEncoderPtr ptr)
125 {
126         drmFree(ptr);
127 }
128
129 /*
130  * ModeSetting functions.
131  */
132
133 drmModeResPtr drmModeGetResources(int fd)
134 {
135         struct drm_mode_card_res res;
136         drmModeResPtr r = 0;
137
138         memset(&res, 0, sizeof(struct drm_mode_card_res));
139
140         if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
141                 return 0;
142
143         if (res.count_fbs)
144                 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
145         if (res.count_crtcs)
146                 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
147         if (res.count_connectors)
148                 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
149         if (res.count_encoders)
150                 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
151
152         if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res)) {
153                 r = NULL;
154                 goto err_allocs;
155         }
156
157         /*
158          * return
159          */
160
161
162         if (!(r = drmMalloc(sizeof(*r))))
163                 return 0;
164
165         r->min_width     = res.min_width;
166         r->max_width     = res.max_width;
167         r->min_height    = res.min_height;
168         r->max_height    = res.max_height;
169         r->count_fbs     = res.count_fbs;
170         r->count_crtcs   = res.count_crtcs;
171         r->count_connectors = res.count_connectors;
172         r->count_encoders = res.count_encoders;
173         /* TODO we realy should test if these allocs fails. */
174         r->fbs           = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
175         r->crtcs         = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
176         r->connectors       = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
177         r->encoders      = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
178
179 err_allocs:
180         drmFree(U642VOID(res.fb_id_ptr));
181         drmFree(U642VOID(res.crtc_id_ptr));
182         drmFree(U642VOID(res.connector_id_ptr));
183         drmFree(U642VOID(res.encoder_id_ptr));
184
185         return r;
186 }
187
188 int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
189                  uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
190                  uint32_t *buf_id)
191 {
192         struct drm_mode_fb_cmd f;
193         int ret;
194
195         f.width  = width;
196         f.height = height;
197         f.pitch  = pitch;
198         f.bpp    = bpp;
199         f.depth  = depth;
200         f.handle = bo_handle;
201
202         if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_ADDFB, &f)))
203                 return ret;
204
205         *buf_id = f.fb_id;
206         return 0;
207 }
208
209 int drmModeRmFB(int fd, uint32_t bufferId)
210 {
211         return drmIoctl(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
212
213
214 }
215
216 drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
217 {
218         struct drm_mode_fb_cmd info;
219         drmModeFBPtr r;
220
221         info.fb_id = buf;
222
223         if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
224                 return NULL;
225
226         if (!(r = drmMalloc(sizeof(*r))))
227                 return NULL;
228
229         r->fb_id = info.fb_id;
230         r->width = info.width;
231         r->height = info.height;
232         r->pitch = info.pitch;
233         r->bpp = info.bpp;
234         r->handle = info.handle;
235         r->depth = info.depth;
236
237         return r;
238 }
239
240
241 /*
242  * Crtc functions
243  */
244
245 drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
246 {
247         struct drm_mode_crtc crtc;
248         drmModeCrtcPtr r;
249
250         crtc.crtc_id = crtcId;
251
252         if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
253                 return 0;
254
255         /*
256          * return
257          */
258
259         if (!(r = drmMalloc(sizeof(*r))))
260                 return 0;
261
262         r->crtc_id         = crtc.crtc_id;
263         r->x               = crtc.x;
264         r->y               = crtc.y;
265         r->mode_valid      = crtc.mode_valid;
266         if (r->mode_valid)
267                 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
268         r->buffer_id       = crtc.fb_id;
269         r->gamma_size      = crtc.gamma_size;
270         return r;
271 }
272
273
274 int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
275                    uint32_t x, uint32_t y, uint32_t *connectors, int count,
276                    drmModeModeInfoPtr mode)
277 {
278         struct drm_mode_crtc crtc;
279
280         crtc.x             = x;
281         crtc.y             = y;
282         crtc.crtc_id       = crtcId;
283         crtc.fb_id         = bufferId;
284         crtc.set_connectors_ptr = VOID2U64(connectors);
285         crtc.count_connectors = count;
286         if (mode) {
287           memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
288           crtc.mode_valid = 1;
289         } else
290           crtc.mode_valid = 0;
291
292         return drmIoctl(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
293 }
294
295 /*
296  * Cursor manipulation
297  */
298
299 int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
300 {
301         struct drm_mode_cursor arg;
302
303         arg.flags = DRM_MODE_CURSOR_BO;
304         arg.crtc_id = crtcId;
305         arg.width = width;
306         arg.height = height;
307         arg.handle = bo_handle;
308
309         return drmIoctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
310 }
311
312 int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
313 {
314         struct drm_mode_cursor arg;
315
316         arg.flags = DRM_MODE_CURSOR_MOVE;
317         arg.crtc_id = crtcId;
318         arg.x = x;
319         arg.y = y;
320
321         return drmIoctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
322 }
323
324 /*
325  * Encoder get
326  */
327 drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
328 {
329         struct drm_mode_get_encoder enc;
330         drmModeEncoderPtr r = NULL;
331
332         enc.encoder_id = encoder_id;
333         enc.encoder_type = 0;
334         enc.possible_crtcs = 0;
335         enc.possible_clones = 0;
336
337         if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
338                 return 0;
339
340         if (!(r = drmMalloc(sizeof(*r))))
341                 return 0;
342
343         r->encoder_id = enc.encoder_id;
344         r->crtc_id = enc.crtc_id;
345         r->encoder_type = enc.encoder_type;
346         r->possible_crtcs = enc.possible_crtcs;
347         r->possible_clones = enc.possible_clones;
348
349         return r;
350 }
351
352 /*
353  * Connector manipulation
354  */
355
356 drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
357 {
358         struct drm_mode_get_connector conn;
359         drmModeConnectorPtr r = NULL;
360
361         conn.connector_id = connector_id;
362         conn.connector_type_id = 0;
363         conn.connector_type  = 0;
364         conn.count_modes  = 0;
365         conn.modes_ptr    = 0;
366         conn.count_props  = 0;
367         conn.props_ptr    = 0;
368         conn.prop_values_ptr = 0;
369         conn.count_encoders  = 0;
370         conn.encoders_ptr = 0;
371
372         if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
373                 return 0;
374
375         if (conn.count_props) {
376                 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
377                 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
378         }
379
380         if (conn.count_modes)
381                 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
382
383         if (conn.count_encoders)
384                 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
385
386         if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
387                 goto err_allocs;
388
389         if(!(r = drmMalloc(sizeof(*r)))) {
390                 goto err_allocs;
391         }
392
393         r->connector_id = conn.connector_id;
394         r->encoder_id = conn.encoder_id;
395         r->connection   = conn.connection;
396         r->mmWidth      = conn.mm_width;
397         r->mmHeight     = conn.mm_height;
398         /* convert subpixel from kernel to userspace */
399         r->subpixel     = conn.subpixel + 1;
400         r->count_modes  = conn.count_modes;
401         /* TODO we should test if these alloc & cpy fails. */
402         r->count_props  = conn.count_props;
403         r->props        = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
404         r->prop_values  = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
405         r->modes        = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
406         r->count_encoders = conn.count_encoders;
407         r->encoders     = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
408         r->connector_type  = conn.connector_type;
409         r->connector_type_id = conn.connector_type_id;
410
411         if (!r->props || !r->prop_values || !r->modes || !r->encoders)
412                 goto err_allocs;
413
414 err_allocs:
415         drmFree(U642VOID(conn.prop_values_ptr));
416         drmFree(U642VOID(conn.props_ptr));
417         drmFree(U642VOID(conn.modes_ptr));
418         drmFree(U642VOID(conn.encoders_ptr));
419
420         return r;
421 }
422
423 int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
424 {
425         struct drm_mode_mode_cmd res;
426
427         memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
428         res.connector_id = connector_id;
429
430         return drmIoctl(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
431 }
432
433 int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
434 {
435         struct drm_mode_mode_cmd res;
436
437         memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
438         res.connector_id = connector_id;
439
440         return drmIoctl(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
441 }
442
443
444 drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
445 {
446         struct drm_mode_get_property prop;
447         drmModePropertyPtr r;
448
449         prop.prop_id = property_id;
450         prop.count_enum_blobs = 0;
451         prop.count_values = 0;
452         prop.flags = 0;
453         prop.enum_blob_ptr = 0;
454         prop.values_ptr = 0;
455
456         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
457                 return 0;
458
459         if (prop.count_values)
460                 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
461
462         if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_ENUM))
463                 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
464
465         if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
466                 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
467                 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
468         }
469
470         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
471                 r = NULL;
472                 goto err_allocs;
473         }
474
475         if (!(r = drmMalloc(sizeof(*r))))
476                 return NULL;
477
478         r->prop_id = prop.prop_id;
479         r->count_values = prop.count_values;
480
481         r->flags = prop.flags;
482         if (prop.count_values)
483                 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
484         if (prop.flags & DRM_MODE_PROP_ENUM) {
485                 r->count_enums = prop.count_enum_blobs;
486                 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
487         } else if (prop.flags & DRM_MODE_PROP_BLOB) {
488                 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
489                 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
490                 r->count_blobs = prop.count_enum_blobs;
491         }
492         strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
493         r->name[DRM_PROP_NAME_LEN-1] = 0;
494
495 err_allocs:
496         drmFree(U642VOID(prop.values_ptr));
497         drmFree(U642VOID(prop.enum_blob_ptr));
498
499         return r;
500 }
501
502 void drmModeFreeProperty(drmModePropertyPtr ptr)
503 {
504         if (!ptr)
505                 return;
506
507         drmFree(ptr->values);
508         drmFree(ptr->enums);
509         drmFree(ptr);
510 }
511
512 drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
513 {
514         struct drm_mode_get_blob blob;
515         drmModePropertyBlobPtr r;
516
517         blob.length = 0;
518         blob.data = 0;
519         blob.blob_id = blob_id;
520
521         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
522                 return NULL;
523
524         if (blob.length)
525                 blob.data = VOID2U64(drmMalloc(blob.length));
526
527         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
528                 r = NULL;
529                 goto err_allocs;
530         }
531
532         if (!(r = drmMalloc(sizeof(*r))))
533                 return NULL;
534
535         r->id = blob.blob_id;
536         r->length = blob.length;
537         r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
538
539 err_allocs:
540         drmFree(U642VOID(blob.data));
541         return r;
542 }
543
544 void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
545 {
546         if (!ptr)
547                 return;
548
549         drmFree(ptr->data);
550         drmFree(ptr);
551 }
552
553 int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
554                              uint64_t value)
555 {
556         struct drm_mode_connector_set_property osp;
557         int ret;
558
559         osp.connector_id = connector_id;
560         osp.prop_id = property_id;
561         osp.value = value;
562
563         if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp)))
564                 return ret;
565
566         return 0;
567 }
568
569 /*
570  * checks if a modesetting capable driver has attached to the pci id
571  * returns 0 if modesetting supported.
572  *  -EINVAL or invalid bus id
573  *  -ENOSYS if no modesetting support
574 */
575 int drmCheckModesettingSupported(const char *busid)
576 {
577 #ifdef __linux__
578         char pci_dev_dir[1024];
579         int domain, bus, dev, func;
580         DIR *sysdir;
581         struct dirent *dent;
582         int found = 0, ret;
583
584         ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
585         if (ret != 4)
586                 return -EINVAL;
587
588         sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
589                 domain, bus, dev, func);
590
591         sysdir = opendir(pci_dev_dir);
592         if (sysdir) {
593                 dent = readdir(sysdir);
594                 while (dent) {
595                         if (!strncmp(dent->d_name, "controlD", 8)) {
596                                 found = 1;
597                                 break;
598                         }
599
600                         dent = readdir(sysdir);
601                 }
602                 closedir(sysdir);
603                 if (found)
604                         return 0;
605         }
606
607         sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
608                 domain, bus, dev, func);
609
610         sysdir = opendir(pci_dev_dir);
611         if (!sysdir)
612                 return -EINVAL;
613
614         dent = readdir(sysdir);
615         while (dent) {
616                 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
617                         found = 1;
618                         break;
619                 }
620
621                 dent = readdir(sysdir);
622         }
623
624         closedir(sysdir);
625         if (found)
626                 return 0;
627 #endif
628         return -ENOSYS;
629
630 }
631
632 int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
633                         uint16_t *red, uint16_t *green, uint16_t *blue)
634 {
635         int ret;
636         struct drm_mode_crtc_lut l;
637
638         l.crtc_id = crtc_id;
639         l.gamma_size = size;
640         l.red = VOID2U64(red);
641         l.green = VOID2U64(green);
642         l.blue = VOID2U64(blue);
643
644         if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_GETGAMMA, &l)))
645                 return ret;
646
647         return 0;
648 }
649
650 int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
651                         uint16_t *red, uint16_t *green, uint16_t *blue)
652 {
653         int ret;
654         struct drm_mode_crtc_lut l;
655
656         l.crtc_id = crtc_id;
657         l.gamma_size = size;
658         l.red = VOID2U64(red);
659         l.green = VOID2U64(green);
660         l.blue = VOID2U64(blue);
661
662         if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_SETGAMMA, &l)))
663                 return ret;
664
665         return 0;
666 }