OSDN Git Service

intel: Don't copy dirty data out when freeing a BO in the fake bufmgr.
[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         r->subpixel     = conn.subpixel;
399         r->count_modes  = conn.count_modes;
400         /* TODO we should test if these alloc & cpy fails. */
401         r->count_props  = conn.count_props;
402         r->props        = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
403         r->prop_values  = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
404         r->modes        = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
405         r->count_encoders = conn.count_encoders;
406         r->encoders     = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
407         r->connector_type  = conn.connector_type;
408         r->connector_type_id = conn.connector_type_id;
409
410         if (!r->props || !r->prop_values || !r->modes || !r->encoders)
411                 goto err_allocs;
412
413 err_allocs:
414         drmFree(U642VOID(conn.prop_values_ptr));
415         drmFree(U642VOID(conn.props_ptr));
416         drmFree(U642VOID(conn.modes_ptr));
417         drmFree(U642VOID(conn.encoders_ptr));
418
419         return r;
420 }
421
422 int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
423 {
424         struct drm_mode_mode_cmd res;
425
426         memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
427         res.connector_id = connector_id;
428
429         return drmIoctl(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
430 }
431
432 int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
433 {
434         struct drm_mode_mode_cmd res;
435
436         memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
437         res.connector_id = connector_id;
438
439         return drmIoctl(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
440 }
441
442
443 drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
444 {
445         struct drm_mode_get_property prop;
446         drmModePropertyPtr r;
447
448         prop.prop_id = property_id;
449         prop.count_enum_blobs = 0;
450         prop.count_values = 0;
451         prop.flags = 0;
452         prop.enum_blob_ptr = 0;
453         prop.values_ptr = 0;
454
455         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
456                 return 0;
457
458         if (prop.count_values)
459                 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
460
461         if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_ENUM))
462                 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
463
464         if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
465                 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
466                 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
467         }
468
469         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
470                 r = NULL;
471                 goto err_allocs;
472         }
473
474         if (!(r = drmMalloc(sizeof(*r))))
475                 return NULL;
476
477         r->prop_id = prop.prop_id;
478         r->count_values = prop.count_values;
479
480         r->flags = prop.flags;
481         if (prop.count_values)
482                 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
483         if (prop.flags & DRM_MODE_PROP_ENUM) {
484                 r->count_enums = prop.count_enum_blobs;
485                 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
486         } else if (prop.flags & DRM_MODE_PROP_BLOB) {
487                 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
488                 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
489                 r->count_blobs = prop.count_enum_blobs;
490         }
491         strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
492         r->name[DRM_PROP_NAME_LEN-1] = 0;
493
494 err_allocs:
495         drmFree(U642VOID(prop.values_ptr));
496         drmFree(U642VOID(prop.enum_blob_ptr));
497
498         return r;
499 }
500
501 void drmModeFreeProperty(drmModePropertyPtr ptr)
502 {
503         if (!ptr)
504                 return;
505
506         drmFree(ptr->values);
507         drmFree(ptr->enums);
508         drmFree(ptr);
509 }
510
511 drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
512 {
513         struct drm_mode_get_blob blob;
514         drmModePropertyBlobPtr r;
515
516         blob.length = 0;
517         blob.data = 0;
518         blob.blob_id = blob_id;
519
520         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
521                 return NULL;
522
523         if (blob.length)
524                 blob.data = VOID2U64(drmMalloc(blob.length));
525
526         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
527                 r = NULL;
528                 goto err_allocs;
529         }
530
531         if (!(r = drmMalloc(sizeof(*r))))
532                 return NULL;
533
534         r->id = blob.blob_id;
535         r->length = blob.length;
536         r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
537
538 err_allocs:
539         drmFree(U642VOID(blob.data));
540         return r;
541 }
542
543 void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
544 {
545         if (!ptr)
546                 return;
547
548         drmFree(ptr->data);
549         drmFree(ptr);
550 }
551
552 int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
553                              uint64_t value)
554 {
555         struct drm_mode_connector_set_property osp;
556         int ret;
557
558         osp.connector_id = connector_id;
559         osp.prop_id = property_id;
560         osp.value = value;
561
562         if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp)))
563                 return ret;
564
565         return 0;
566 }
567
568 /*
569  * checks if a modesetting capable driver has attached to the pci id
570  * returns 0 if modesetting supported.
571  *  -EINVAL or invalid bus id
572  *  -ENOSYS if no modesetting support
573 */
574 int drmCheckModesettingSupported(const char *busid)
575 {
576 #ifdef __linux__
577         char pci_dev_dir[1024];
578         int domain, bus, dev, func;
579         DIR *sysdir;
580         struct dirent *dent;
581         int found = 0, ret;
582
583         ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
584         if (ret != 4)
585                 return -EINVAL;
586
587         sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
588                 domain, bus, dev, func);
589
590         sysdir = opendir(pci_dev_dir);
591         if (sysdir) {
592                 dent = readdir(sysdir);
593                 while (dent) {
594                         if (!strncmp(dent->d_name, "controlD", 8)) {
595                                 found = 1;
596                                 break;
597                         }
598
599                         dent = readdir(sysdir);
600                 }
601                 closedir(sysdir);
602                 if (found)
603                         return 0;
604         }
605
606         sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
607                 domain, bus, dev, func);
608
609         sysdir = opendir(pci_dev_dir);
610         if (!sysdir)
611                 return -EINVAL;
612
613         dent = readdir(sysdir);
614         while (dent) {
615                 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
616                         found = 1;
617                         break;
618                 }
619
620                 dent = readdir(sysdir);
621         }
622
623         closedir(sysdir);
624         if (found)
625                 return 0;
626 #endif
627         return -ENOSYS;
628
629 }
630
631 int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
632                         uint16_t *red, uint16_t *green, uint16_t *blue)
633 {
634         int ret;
635         struct drm_mode_crtc_lut l;
636
637         l.crtc_id = crtc_id;
638         l.gamma_size = size;
639         l.red = VOID2U64(red);
640         l.green = VOID2U64(green);
641         l.blue = VOID2U64(blue);
642
643         if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_GETGAMMA, &l)))
644                 return ret;
645
646         return 0;
647 }
648
649 int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
650                         uint16_t *red, uint16_t *green, uint16_t *blue)
651 {
652         int ret;
653         struct drm_mode_crtc_lut l;
654
655         l.crtc_id = crtc_id;
656         l.gamma_size = size;
657         l.red = VOID2U64(red);
658         l.green = VOID2U64(green);
659         l.blue = VOID2U64(blue);
660
661         if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_SETGAMMA, &l)))
662                 return ret;
663
664         return 0;
665 }