drm_bufs.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  1. /**
  2. * \file drm_bufs.c
  3. * Generic buffer template
  4. *
  5. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  6. * \author Gareth Hughes <gareth@valinux.com>
  7. */
  8. /*
  9. * Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com
  10. *
  11. * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
  12. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  13. * All Rights Reserved.
  14. *
  15. * Permission is hereby granted, free of charge, to any person obtaining a
  16. * copy of this software and associated documentation files (the "Software"),
  17. * to deal in the Software without restriction, including without limitation
  18. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  19. * and/or sell copies of the Software, and to permit persons to whom the
  20. * Software is furnished to do so, subject to the following conditions:
  21. *
  22. * The above copyright notice and this permission notice (including the next
  23. * paragraph) shall be included in all copies or substantial portions of the
  24. * 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
  29. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  30. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  31. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  32. * OTHER DEALINGS IN THE SOFTWARE.
  33. */
  34. #include <linux/vmalloc.h>
  35. #include "drmP.h"
  36. unsigned long drm_get_resource_start(drm_device_t *dev, unsigned int resource)
  37. {
  38. return pci_resource_start(dev->pdev, resource);
  39. }
  40. EXPORT_SYMBOL(drm_get_resource_start);
  41. unsigned long drm_get_resource_len(drm_device_t *dev, unsigned int resource)
  42. {
  43. return pci_resource_len(dev->pdev, resource);
  44. }
  45. EXPORT_SYMBOL(drm_get_resource_len);
  46. static drm_map_list_t *drm_find_matching_map(drm_device_t *dev,
  47. drm_local_map_t *map)
  48. {
  49. struct list_head *list;
  50. list_for_each(list, &dev->maplist->head) {
  51. drm_map_list_t *entry = list_entry(list, drm_map_list_t, head);
  52. if (entry->map && map->type == entry->map->type &&
  53. entry->map->offset == map->offset) {
  54. return entry;
  55. }
  56. }
  57. return NULL;
  58. }
  59. /*
  60. * Used to allocate 32-bit handles for mappings.
  61. */
  62. #define START_RANGE 0x10000000
  63. #define END_RANGE 0x40000000
  64. #ifdef _LP64
  65. static __inline__ unsigned int HandleID(unsigned long lhandle,
  66. drm_device_t *dev)
  67. {
  68. static unsigned int map32_handle = START_RANGE;
  69. unsigned int hash;
  70. if (lhandle & 0xffffffff00000000) {
  71. hash = map32_handle;
  72. map32_handle += PAGE_SIZE;
  73. if (map32_handle > END_RANGE)
  74. map32_handle = START_RANGE;
  75. } else
  76. hash = lhandle;
  77. while (1) {
  78. drm_map_list_t *_entry;
  79. list_for_each_entry(_entry, &dev->maplist->head, head) {
  80. if (_entry->user_token == hash)
  81. break;
  82. }
  83. if (&_entry->head == &dev->maplist->head)
  84. return hash;
  85. hash += PAGE_SIZE;
  86. map32_handle += PAGE_SIZE;
  87. }
  88. }
  89. #else
  90. # define HandleID(x,dev) (unsigned int)(x)
  91. #endif
  92. /**
  93. * Ioctl to specify a range of memory that is available for mapping by a non-root process.
  94. *
  95. * \param inode device inode.
  96. * \param filp file pointer.
  97. * \param cmd command.
  98. * \param arg pointer to a drm_map structure.
  99. * \return zero on success or a negative value on error.
  100. *
  101. * Adjusts the memory offset to its absolute value according to the mapping
  102. * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
  103. * applicable and if supported by the kernel.
  104. */
  105. static int drm_addmap_core(drm_device_t * dev, unsigned int offset,
  106. unsigned int size, drm_map_type_t type,
  107. drm_map_flags_t flags, drm_map_list_t ** maplist)
  108. {
  109. drm_map_t *map;
  110. drm_map_list_t *list;
  111. drm_dma_handle_t *dmah;
  112. map = drm_alloc(sizeof(*map), DRM_MEM_MAPS);
  113. if (!map)
  114. return -ENOMEM;
  115. map->offset = offset;
  116. map->size = size;
  117. map->flags = flags;
  118. map->type = type;
  119. /* Only allow shared memory to be removable since we only keep enough
  120. * book keeping information about shared memory to allow for removal
  121. * when processes fork.
  122. */
  123. if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
  124. drm_free(map, sizeof(*map), DRM_MEM_MAPS);
  125. return -EINVAL;
  126. }
  127. DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n",
  128. map->offset, map->size, map->type);
  129. if ((map->offset & (~PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
  130. drm_free(map, sizeof(*map), DRM_MEM_MAPS);
  131. return -EINVAL;
  132. }
  133. map->mtrr = -1;
  134. map->handle = NULL;
  135. switch (map->type) {
  136. case _DRM_REGISTERS:
  137. case _DRM_FRAME_BUFFER:
  138. #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
  139. if (map->offset + (map->size-1) < map->offset ||
  140. map->offset < virt_to_phys(high_memory)) {
  141. drm_free(map, sizeof(*map), DRM_MEM_MAPS);
  142. return -EINVAL;
  143. }
  144. #endif
  145. #ifdef __alpha__
  146. map->offset += dev->hose->mem_space->start;
  147. #endif
  148. /* Some drivers preinitialize some maps, without the X Server
  149. * needing to be aware of it. Therefore, we just return success
  150. * when the server tries to create a duplicate map.
  151. */
  152. list = drm_find_matching_map(dev, map);
  153. if (list != NULL) {
  154. if (list->map->size != map->size) {
  155. DRM_DEBUG("Matching maps of type %d with "
  156. "mismatched sizes, (%ld vs %ld)\n",
  157. map->type, map->size,
  158. list->map->size);
  159. list->map->size = map->size;
  160. }
  161. drm_free(map, sizeof(*map), DRM_MEM_MAPS);
  162. *maplist = list;
  163. return 0;
  164. }
  165. if (drm_core_has_MTRR(dev)) {
  166. if (map->type == _DRM_FRAME_BUFFER ||
  167. (map->flags & _DRM_WRITE_COMBINING)) {
  168. map->mtrr = mtrr_add(map->offset, map->size,
  169. MTRR_TYPE_WRCOMB, 1);
  170. }
  171. }
  172. if (map->type == _DRM_REGISTERS)
  173. map->handle = drm_ioremap(map->offset, map->size, dev);
  174. break;
  175. case _DRM_SHM:
  176. map->handle = vmalloc_32(map->size);
  177. DRM_DEBUG("%lu %d %p\n",
  178. map->size, drm_order(map->size), map->handle);
  179. if (!map->handle) {
  180. drm_free(map, sizeof(*map), DRM_MEM_MAPS);
  181. return -ENOMEM;
  182. }
  183. map->offset = (unsigned long)map->handle;
  184. if (map->flags & _DRM_CONTAINS_LOCK) {
  185. /* Prevent a 2nd X Server from creating a 2nd lock */
  186. if (dev->lock.hw_lock != NULL) {
  187. vfree(map->handle);
  188. drm_free(map, sizeof(*map), DRM_MEM_MAPS);
  189. return -EBUSY;
  190. }
  191. dev->sigdata.lock = dev->lock.hw_lock = map->handle; /* Pointer to lock */
  192. }
  193. break;
  194. case _DRM_AGP:
  195. if (drm_core_has_AGP(dev)) {
  196. #ifdef __alpha__
  197. map->offset += dev->hose->mem_space->start;
  198. #endif
  199. map->offset += dev->agp->base;
  200. map->mtrr = dev->agp->agp_mtrr; /* for getmap */
  201. }
  202. break;
  203. case _DRM_SCATTER_GATHER:
  204. if (!dev->sg) {
  205. drm_free(map, sizeof(*map), DRM_MEM_MAPS);
  206. return -EINVAL;
  207. }
  208. map->offset += (unsigned long)dev->sg->virtual;
  209. break;
  210. case _DRM_CONSISTENT:
  211. /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
  212. * As we're limiting the address to 2^32-1 (or less),
  213. * casting it down to 32 bits is no problem, but we
  214. * need to point to a 64bit variable first. */
  215. dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
  216. if (!dmah) {
  217. drm_free(map, sizeof(*map), DRM_MEM_MAPS);
  218. return -ENOMEM;
  219. }
  220. map->handle = dmah->vaddr;
  221. map->offset = (unsigned long)dmah->busaddr;
  222. kfree(dmah);
  223. break;
  224. default:
  225. drm_free(map, sizeof(*map), DRM_MEM_MAPS);
  226. return -EINVAL;
  227. }
  228. list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
  229. if (!list) {
  230. drm_free(map, sizeof(*map), DRM_MEM_MAPS);
  231. return -EINVAL;
  232. }
  233. memset(list, 0, sizeof(*list));
  234. list->map = map;
  235. mutex_lock(&dev->struct_mutex);
  236. list_add(&list->head, &dev->maplist->head);
  237. /* Assign a 32-bit handle */
  238. /* We do it here so that dev->struct_mutex protects the increment */
  239. list->user_token = HandleID(map->type == _DRM_SHM
  240. ? (unsigned long)map->handle
  241. : map->offset, dev);
  242. mutex_unlock(&dev->struct_mutex);
  243. *maplist = list;
  244. return 0;
  245. }
  246. int drm_addmap(drm_device_t * dev, unsigned int offset,
  247. unsigned int size, drm_map_type_t type,
  248. drm_map_flags_t flags, drm_local_map_t ** map_ptr)
  249. {
  250. drm_map_list_t *list;
  251. int rc;
  252. rc = drm_addmap_core(dev, offset, size, type, flags, &list);
  253. if (!rc)
  254. *map_ptr = list->map;
  255. return rc;
  256. }
  257. EXPORT_SYMBOL(drm_addmap);
  258. int drm_addmap_ioctl(struct inode *inode, struct file *filp,
  259. unsigned int cmd, unsigned long arg)
  260. {
  261. drm_file_t *priv = filp->private_data;
  262. drm_device_t *dev = priv->head->dev;
  263. drm_map_t map;
  264. drm_map_list_t *maplist;
  265. drm_map_t __user *argp = (void __user *)arg;
  266. int err;
  267. if (!(filp->f_mode & 3))
  268. return -EACCES; /* Require read/write */
  269. if (copy_from_user(&map, argp, sizeof(map))) {
  270. return -EFAULT;
  271. }
  272. if (!(capable(CAP_SYS_ADMIN) || map.type == _DRM_AGP))
  273. return -EPERM;
  274. err = drm_addmap_core(dev, map.offset, map.size, map.type, map.flags,
  275. &maplist);
  276. if (err)
  277. return err;
  278. if (copy_to_user(argp, maplist->map, sizeof(drm_map_t)))
  279. return -EFAULT;
  280. /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
  281. if (put_user((void *)(unsigned long)maplist->user_token, &argp->handle))
  282. return -EFAULT;
  283. return 0;
  284. }
  285. /**
  286. * Remove a map private from list and deallocate resources if the mapping
  287. * isn't in use.
  288. *
  289. * \param inode device inode.
  290. * \param filp file pointer.
  291. * \param cmd command.
  292. * \param arg pointer to a drm_map_t structure.
  293. * \return zero on success or a negative value on error.
  294. *
  295. * Searches the map on drm_device::maplist, removes it from the list, see if
  296. * its being used, and free any associate resource (such as MTRR's) if it's not
  297. * being on use.
  298. *
  299. * \sa drm_addmap
  300. */
  301. int drm_rmmap_locked(drm_device_t *dev, drm_local_map_t *map)
  302. {
  303. struct list_head *list;
  304. drm_map_list_t *r_list = NULL;
  305. drm_dma_handle_t dmah;
  306. /* Find the list entry for the map and remove it */
  307. list_for_each(list, &dev->maplist->head) {
  308. r_list = list_entry(list, drm_map_list_t, head);
  309. if (r_list->map == map) {
  310. list_del(list);
  311. drm_free(list, sizeof(*list), DRM_MEM_MAPS);
  312. break;
  313. }
  314. }
  315. /* List has wrapped around to the head pointer, or it's empty and we
  316. * didn't find anything.
  317. */
  318. if (list == (&dev->maplist->head)) {
  319. return -EINVAL;
  320. }
  321. switch (map->type) {
  322. case _DRM_REGISTERS:
  323. drm_ioremapfree(map->handle, map->size, dev);
  324. /* FALLTHROUGH */
  325. case _DRM_FRAME_BUFFER:
  326. if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
  327. int retcode;
  328. retcode = mtrr_del(map->mtrr, map->offset, map->size);
  329. DRM_DEBUG("mtrr_del=%d\n", retcode);
  330. }
  331. break;
  332. case _DRM_SHM:
  333. vfree(map->handle);
  334. break;
  335. case _DRM_AGP:
  336. case _DRM_SCATTER_GATHER:
  337. break;
  338. case _DRM_CONSISTENT:
  339. dmah.vaddr = map->handle;
  340. dmah.busaddr = map->offset;
  341. dmah.size = map->size;
  342. __drm_pci_free(dev, &dmah);
  343. break;
  344. }
  345. drm_free(map, sizeof(*map), DRM_MEM_MAPS);
  346. return 0;
  347. }
  348. int drm_rmmap(drm_device_t *dev, drm_local_map_t *map)
  349. {
  350. int ret;
  351. mutex_lock(&dev->struct_mutex);
  352. ret = drm_rmmap_locked(dev, map);
  353. mutex_unlock(&dev->struct_mutex);
  354. return ret;
  355. }
  356. /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
  357. * the last close of the device, and this is necessary for cleanup when things
  358. * exit uncleanly. Therefore, having userland manually remove mappings seems
  359. * like a pointless exercise since they're going away anyway.
  360. *
  361. * One use case might be after addmap is allowed for normal users for SHM and
  362. * gets used by drivers that the server doesn't need to care about. This seems
  363. * unlikely.
  364. */
  365. int drm_rmmap_ioctl(struct inode *inode, struct file *filp,
  366. unsigned int cmd, unsigned long arg)
  367. {
  368. drm_file_t *priv = filp->private_data;
  369. drm_device_t *dev = priv->head->dev;
  370. drm_map_t request;
  371. drm_local_map_t *map = NULL;
  372. struct list_head *list;
  373. int ret;
  374. if (copy_from_user(&request, (drm_map_t __user *) arg, sizeof(request))) {
  375. return -EFAULT;
  376. }
  377. mutex_lock(&dev->struct_mutex);
  378. list_for_each(list, &dev->maplist->head) {
  379. drm_map_list_t *r_list = list_entry(list, drm_map_list_t, head);
  380. if (r_list->map &&
  381. r_list->user_token == (unsigned long)request.handle &&
  382. r_list->map->flags & _DRM_REMOVABLE) {
  383. map = r_list->map;
  384. break;
  385. }
  386. }
  387. /* List has wrapped around to the head pointer, or its empty we didn't
  388. * find anything.
  389. */
  390. if (list == (&dev->maplist->head)) {
  391. mutex_unlock(&dev->struct_mutex);
  392. return -EINVAL;
  393. }
  394. if (!map) {
  395. mutex_unlock(&dev->struct_mutex);
  396. return -EINVAL;
  397. }
  398. /* Register and framebuffer maps are permanent */
  399. if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
  400. mutex_unlock(&dev->struct_mutex);
  401. return 0;
  402. }
  403. ret = drm_rmmap_locked(dev, map);
  404. mutex_unlock(&dev->struct_mutex);
  405. return ret;
  406. }
  407. /**
  408. * Cleanup after an error on one of the addbufs() functions.
  409. *
  410. * \param dev DRM device.
  411. * \param entry buffer entry where the error occurred.
  412. *
  413. * Frees any pages and buffers associated with the given entry.
  414. */
  415. static void drm_cleanup_buf_error(drm_device_t * dev, drm_buf_entry_t * entry)
  416. {
  417. int i;
  418. if (entry->seg_count) {
  419. for (i = 0; i < entry->seg_count; i++) {
  420. if (entry->seglist[i]) {
  421. drm_pci_free(dev, entry->seglist[i]);
  422. }
  423. }
  424. drm_free(entry->seglist,
  425. entry->seg_count *
  426. sizeof(*entry->seglist), DRM_MEM_SEGS);
  427. entry->seg_count = 0;
  428. }
  429. if (entry->buf_count) {
  430. for (i = 0; i < entry->buf_count; i++) {
  431. if (entry->buflist[i].dev_private) {
  432. drm_free(entry->buflist[i].dev_private,
  433. entry->buflist[i].dev_priv_size,
  434. DRM_MEM_BUFS);
  435. }
  436. }
  437. drm_free(entry->buflist,
  438. entry->buf_count *
  439. sizeof(*entry->buflist), DRM_MEM_BUFS);
  440. entry->buf_count = 0;
  441. }
  442. }
  443. #if __OS_HAS_AGP
  444. /**
  445. * Add AGP buffers for DMA transfers.
  446. *
  447. * \param dev drm_device_t to which the buffers are to be added.
  448. * \param request pointer to a drm_buf_desc_t describing the request.
  449. * \return zero on success or a negative number on failure.
  450. *
  451. * After some sanity checks creates a drm_buf structure for each buffer and
  452. * reallocates the buffer list of the same size order to accommodate the new
  453. * buffers.
  454. */
  455. int drm_addbufs_agp(drm_device_t * dev, drm_buf_desc_t * request)
  456. {
  457. drm_device_dma_t *dma = dev->dma;
  458. drm_buf_entry_t *entry;
  459. drm_buf_t *buf;
  460. unsigned long offset;
  461. unsigned long agp_offset;
  462. int count;
  463. int order;
  464. int size;
  465. int alignment;
  466. int page_order;
  467. int total;
  468. int byte_count;
  469. int i;
  470. drm_buf_t **temp_buflist;
  471. if (!dma)
  472. return -EINVAL;
  473. count = request->count;
  474. order = drm_order(request->size);
  475. size = 1 << order;
  476. alignment = (request->flags & _DRM_PAGE_ALIGN)
  477. ? PAGE_ALIGN(size) : size;
  478. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  479. total = PAGE_SIZE << page_order;
  480. byte_count = 0;
  481. agp_offset = dev->agp->base + request->agp_start;
  482. DRM_DEBUG("count: %d\n", count);
  483. DRM_DEBUG("order: %d\n", order);
  484. DRM_DEBUG("size: %d\n", size);
  485. DRM_DEBUG("agp_offset: %lx\n", agp_offset);
  486. DRM_DEBUG("alignment: %d\n", alignment);
  487. DRM_DEBUG("page_order: %d\n", page_order);
  488. DRM_DEBUG("total: %d\n", total);
  489. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  490. return -EINVAL;
  491. if (dev->queue_count)
  492. return -EBUSY; /* Not while in use */
  493. spin_lock(&dev->count_lock);
  494. if (dev->buf_use) {
  495. spin_unlock(&dev->count_lock);
  496. return -EBUSY;
  497. }
  498. atomic_inc(&dev->buf_alloc);
  499. spin_unlock(&dev->count_lock);
  500. mutex_lock(&dev->struct_mutex);
  501. entry = &dma->bufs[order];
  502. if (entry->buf_count) {
  503. mutex_unlock(&dev->struct_mutex);
  504. atomic_dec(&dev->buf_alloc);
  505. return -ENOMEM; /* May only call once for each order */
  506. }
  507. if (count < 0 || count > 4096) {
  508. mutex_unlock(&dev->struct_mutex);
  509. atomic_dec(&dev->buf_alloc);
  510. return -EINVAL;
  511. }
  512. entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
  513. DRM_MEM_BUFS);
  514. if (!entry->buflist) {
  515. mutex_unlock(&dev->struct_mutex);
  516. atomic_dec(&dev->buf_alloc);
  517. return -ENOMEM;
  518. }
  519. memset(entry->buflist, 0, count * sizeof(*entry->buflist));
  520. entry->buf_size = size;
  521. entry->page_order = page_order;
  522. offset = 0;
  523. while (entry->buf_count < count) {
  524. buf = &entry->buflist[entry->buf_count];
  525. buf->idx = dma->buf_count + entry->buf_count;
  526. buf->total = alignment;
  527. buf->order = order;
  528. buf->used = 0;
  529. buf->offset = (dma->byte_count + offset);
  530. buf->bus_address = agp_offset + offset;
  531. buf->address = (void *)(agp_offset + offset);
  532. buf->next = NULL;
  533. buf->waiting = 0;
  534. buf->pending = 0;
  535. init_waitqueue_head(&buf->dma_wait);
  536. buf->filp = NULL;
  537. buf->dev_priv_size = dev->driver->dev_priv_size;
  538. buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
  539. if (!buf->dev_private) {
  540. /* Set count correctly so we free the proper amount. */
  541. entry->buf_count = count;
  542. drm_cleanup_buf_error(dev, entry);
  543. mutex_unlock(&dev->struct_mutex);
  544. atomic_dec(&dev->buf_alloc);
  545. return -ENOMEM;
  546. }
  547. memset(buf->dev_private, 0, buf->dev_priv_size);
  548. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  549. offset += alignment;
  550. entry->buf_count++;
  551. byte_count += PAGE_SIZE << page_order;
  552. }
  553. DRM_DEBUG("byte_count: %d\n", byte_count);
  554. temp_buflist = drm_realloc(dma->buflist,
  555. dma->buf_count * sizeof(*dma->buflist),
  556. (dma->buf_count + entry->buf_count)
  557. * sizeof(*dma->buflist), DRM_MEM_BUFS);
  558. if (!temp_buflist) {
  559. /* Free the entry because it isn't valid */
  560. drm_cleanup_buf_error(dev, entry);
  561. mutex_unlock(&dev->struct_mutex);
  562. atomic_dec(&dev->buf_alloc);
  563. return -ENOMEM;
  564. }
  565. dma->buflist = temp_buflist;
  566. for (i = 0; i < entry->buf_count; i++) {
  567. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  568. }
  569. dma->buf_count += entry->buf_count;
  570. dma->seg_count += entry->seg_count;
  571. dma->page_count += byte_count >> PAGE_SHIFT;
  572. dma->byte_count += byte_count;
  573. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  574. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  575. mutex_unlock(&dev->struct_mutex);
  576. request->count = entry->buf_count;
  577. request->size = size;
  578. dma->flags = _DRM_DMA_USE_AGP;
  579. atomic_dec(&dev->buf_alloc);
  580. return 0;
  581. }
  582. EXPORT_SYMBOL(drm_addbufs_agp);
  583. #endif /* __OS_HAS_AGP */
  584. int drm_addbufs_pci(drm_device_t * dev, drm_buf_desc_t * request)
  585. {
  586. drm_device_dma_t *dma = dev->dma;
  587. int count;
  588. int order;
  589. int size;
  590. int total;
  591. int page_order;
  592. drm_buf_entry_t *entry;
  593. drm_dma_handle_t *dmah;
  594. drm_buf_t *buf;
  595. int alignment;
  596. unsigned long offset;
  597. int i;
  598. int byte_count;
  599. int page_count;
  600. unsigned long *temp_pagelist;
  601. drm_buf_t **temp_buflist;
  602. if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
  603. return -EINVAL;
  604. if (!dma)
  605. return -EINVAL;
  606. if (!capable(CAP_SYS_ADMIN))
  607. return -EPERM;
  608. count = request->count;
  609. order = drm_order(request->size);
  610. size = 1 << order;
  611. DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
  612. request->count, request->size, size, order, dev->queue_count);
  613. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  614. return -EINVAL;
  615. if (dev->queue_count)
  616. return -EBUSY; /* Not while in use */
  617. alignment = (request->flags & _DRM_PAGE_ALIGN)
  618. ? PAGE_ALIGN(size) : size;
  619. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  620. total = PAGE_SIZE << page_order;
  621. spin_lock(&dev->count_lock);
  622. if (dev->buf_use) {
  623. spin_unlock(&dev->count_lock);
  624. return -EBUSY;
  625. }
  626. atomic_inc(&dev->buf_alloc);
  627. spin_unlock(&dev->count_lock);
  628. mutex_lock(&dev->struct_mutex);
  629. entry = &dma->bufs[order];
  630. if (entry->buf_count) {
  631. mutex_unlock(&dev->struct_mutex);
  632. atomic_dec(&dev->buf_alloc);
  633. return -ENOMEM; /* May only call once for each order */
  634. }
  635. if (count < 0 || count > 4096) {
  636. mutex_unlock(&dev->struct_mutex);
  637. atomic_dec(&dev->buf_alloc);
  638. return -EINVAL;
  639. }
  640. entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
  641. DRM_MEM_BUFS);
  642. if (!entry->buflist) {
  643. mutex_unlock(&dev->struct_mutex);
  644. atomic_dec(&dev->buf_alloc);
  645. return -ENOMEM;
  646. }
  647. memset(entry->buflist, 0, count * sizeof(*entry->buflist));
  648. entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
  649. DRM_MEM_SEGS);
  650. if (!entry->seglist) {
  651. drm_free(entry->buflist,
  652. count * sizeof(*entry->buflist), DRM_MEM_BUFS);
  653. mutex_unlock(&dev->struct_mutex);
  654. atomic_dec(&dev->buf_alloc);
  655. return -ENOMEM;
  656. }
  657. memset(entry->seglist, 0, count * sizeof(*entry->seglist));
  658. /* Keep the original pagelist until we know all the allocations
  659. * have succeeded
  660. */
  661. temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
  662. * sizeof(*dma->pagelist), DRM_MEM_PAGES);
  663. if (!temp_pagelist) {
  664. drm_free(entry->buflist,
  665. count * sizeof(*entry->buflist), DRM_MEM_BUFS);
  666. drm_free(entry->seglist,
  667. count * sizeof(*entry->seglist), DRM_MEM_SEGS);
  668. mutex_unlock(&dev->struct_mutex);
  669. atomic_dec(&dev->buf_alloc);
  670. return -ENOMEM;
  671. }
  672. memcpy(temp_pagelist,
  673. dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
  674. DRM_DEBUG("pagelist: %d entries\n",
  675. dma->page_count + (count << page_order));
  676. entry->buf_size = size;
  677. entry->page_order = page_order;
  678. byte_count = 0;
  679. page_count = 0;
  680. while (entry->buf_count < count) {
  681. dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
  682. if (!dmah) {
  683. /* Set count correctly so we free the proper amount. */
  684. entry->buf_count = count;
  685. entry->seg_count = count;
  686. drm_cleanup_buf_error(dev, entry);
  687. drm_free(temp_pagelist,
  688. (dma->page_count + (count << page_order))
  689. * sizeof(*dma->pagelist), DRM_MEM_PAGES);
  690. mutex_unlock(&dev->struct_mutex);
  691. atomic_dec(&dev->buf_alloc);
  692. return -ENOMEM;
  693. }
  694. entry->seglist[entry->seg_count++] = dmah;
  695. for (i = 0; i < (1 << page_order); i++) {
  696. DRM_DEBUG("page %d @ 0x%08lx\n",
  697. dma->page_count + page_count,
  698. (unsigned long)dmah->vaddr + PAGE_SIZE * i);
  699. temp_pagelist[dma->page_count + page_count++]
  700. = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
  701. }
  702. for (offset = 0;
  703. offset + size <= total && entry->buf_count < count;
  704. offset += alignment, ++entry->buf_count) {
  705. buf = &entry->buflist[entry->buf_count];
  706. buf->idx = dma->buf_count + entry->buf_count;
  707. buf->total = alignment;
  708. buf->order = order;
  709. buf->used = 0;
  710. buf->offset = (dma->byte_count + byte_count + offset);
  711. buf->address = (void *)(dmah->vaddr + offset);
  712. buf->bus_address = dmah->busaddr + offset;
  713. buf->next = NULL;
  714. buf->waiting = 0;
  715. buf->pending = 0;
  716. init_waitqueue_head(&buf->dma_wait);
  717. buf->filp = NULL;
  718. buf->dev_priv_size = dev->driver->dev_priv_size;
  719. buf->dev_private = drm_alloc(buf->dev_priv_size,
  720. DRM_MEM_BUFS);
  721. if (!buf->dev_private) {
  722. /* Set count correctly so we free the proper amount. */
  723. entry->buf_count = count;
  724. entry->seg_count = count;
  725. drm_cleanup_buf_error(dev, entry);
  726. drm_free(temp_pagelist,
  727. (dma->page_count +
  728. (count << page_order))
  729. * sizeof(*dma->pagelist),
  730. DRM_MEM_PAGES);
  731. mutex_unlock(&dev->struct_mutex);
  732. atomic_dec(&dev->buf_alloc);
  733. return -ENOMEM;
  734. }
  735. memset(buf->dev_private, 0, buf->dev_priv_size);
  736. DRM_DEBUG("buffer %d @ %p\n",
  737. entry->buf_count, buf->address);
  738. }
  739. byte_count += PAGE_SIZE << page_order;
  740. }
  741. temp_buflist = drm_realloc(dma->buflist,
  742. dma->buf_count * sizeof(*dma->buflist),
  743. (dma->buf_count + entry->buf_count)
  744. * sizeof(*dma->buflist), DRM_MEM_BUFS);
  745. if (!temp_buflist) {
  746. /* Free the entry because it isn't valid */
  747. drm_cleanup_buf_error(dev, entry);
  748. drm_free(temp_pagelist,
  749. (dma->page_count + (count << page_order))
  750. * sizeof(*dma->pagelist), DRM_MEM_PAGES);
  751. mutex_unlock(&dev->struct_mutex);
  752. atomic_dec(&dev->buf_alloc);
  753. return -ENOMEM;
  754. }
  755. dma->buflist = temp_buflist;
  756. for (i = 0; i < entry->buf_count; i++) {
  757. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  758. }
  759. /* No allocations failed, so now we can replace the orginal pagelist
  760. * with the new one.
  761. */
  762. if (dma->page_count) {
  763. drm_free(dma->pagelist,
  764. dma->page_count * sizeof(*dma->pagelist),
  765. DRM_MEM_PAGES);
  766. }
  767. dma->pagelist = temp_pagelist;
  768. dma->buf_count += entry->buf_count;
  769. dma->seg_count += entry->seg_count;
  770. dma->page_count += entry->seg_count << page_order;
  771. dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
  772. mutex_unlock(&dev->struct_mutex);
  773. request->count = entry->buf_count;
  774. request->size = size;
  775. atomic_dec(&dev->buf_alloc);
  776. return 0;
  777. }
  778. EXPORT_SYMBOL(drm_addbufs_pci);
  779. static int drm_addbufs_sg(drm_device_t * dev, drm_buf_desc_t * request)
  780. {
  781. drm_device_dma_t *dma = dev->dma;
  782. drm_buf_entry_t *entry;
  783. drm_buf_t *buf;
  784. unsigned long offset;
  785. unsigned long agp_offset;
  786. int count;
  787. int order;
  788. int size;
  789. int alignment;
  790. int page_order;
  791. int total;
  792. int byte_count;
  793. int i;
  794. drm_buf_t **temp_buflist;
  795. if (!drm_core_check_feature(dev, DRIVER_SG))
  796. return -EINVAL;
  797. if (!dma)
  798. return -EINVAL;
  799. if (!capable(CAP_SYS_ADMIN))
  800. return -EPERM;
  801. count = request->count;
  802. order = drm_order(request->size);
  803. size = 1 << order;
  804. alignment = (request->flags & _DRM_PAGE_ALIGN)
  805. ? PAGE_ALIGN(size) : size;
  806. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  807. total = PAGE_SIZE << page_order;
  808. byte_count = 0;
  809. agp_offset = request->agp_start;
  810. DRM_DEBUG("count: %d\n", count);
  811. DRM_DEBUG("order: %d\n", order);
  812. DRM_DEBUG("size: %d\n", size);
  813. DRM_DEBUG("agp_offset: %lu\n", agp_offset);
  814. DRM_DEBUG("alignment: %d\n", alignment);
  815. DRM_DEBUG("page_order: %d\n", page_order);
  816. DRM_DEBUG("total: %d\n", total);
  817. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  818. return -EINVAL;
  819. if (dev->queue_count)
  820. return -EBUSY; /* Not while in use */
  821. spin_lock(&dev->count_lock);
  822. if (dev->buf_use) {
  823. spin_unlock(&dev->count_lock);
  824. return -EBUSY;
  825. }
  826. atomic_inc(&dev->buf_alloc);
  827. spin_unlock(&dev->count_lock);
  828. mutex_lock(&dev->struct_mutex);
  829. entry = &dma->bufs[order];
  830. if (entry->buf_count) {
  831. mutex_unlock(&dev->struct_mutex);
  832. atomic_dec(&dev->buf_alloc);
  833. return -ENOMEM; /* May only call once for each order */
  834. }
  835. if (count < 0 || count > 4096) {
  836. mutex_unlock(&dev->struct_mutex);
  837. atomic_dec(&dev->buf_alloc);
  838. return -EINVAL;
  839. }
  840. entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
  841. DRM_MEM_BUFS);
  842. if (!entry->buflist) {
  843. mutex_unlock(&dev->struct_mutex);
  844. atomic_dec(&dev->buf_alloc);
  845. return -ENOMEM;
  846. }
  847. memset(entry->buflist, 0, count * sizeof(*entry->buflist));
  848. entry->buf_size = size;
  849. entry->page_order = page_order;
  850. offset = 0;
  851. while (entry->buf_count < count) {
  852. buf = &entry->buflist[entry->buf_count];
  853. buf->idx = dma->buf_count + entry->buf_count;
  854. buf->total = alignment;
  855. buf->order = order;
  856. buf->used = 0;
  857. buf->offset = (dma->byte_count + offset);
  858. buf->bus_address = agp_offset + offset;
  859. buf->address = (void *)(agp_offset + offset
  860. + (unsigned long)dev->sg->virtual);
  861. buf->next = NULL;
  862. buf->waiting = 0;
  863. buf->pending = 0;
  864. init_waitqueue_head(&buf->dma_wait);
  865. buf->filp = NULL;
  866. buf->dev_priv_size = dev->driver->dev_priv_size;
  867. buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
  868. if (!buf->dev_private) {
  869. /* Set count correctly so we free the proper amount. */
  870. entry->buf_count = count;
  871. drm_cleanup_buf_error(dev, entry);
  872. mutex_unlock(&dev->struct_mutex);
  873. atomic_dec(&dev->buf_alloc);
  874. return -ENOMEM;
  875. }
  876. memset(buf->dev_private, 0, buf->dev_priv_size);
  877. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  878. offset += alignment;
  879. entry->buf_count++;
  880. byte_count += PAGE_SIZE << page_order;
  881. }
  882. DRM_DEBUG("byte_count: %d\n", byte_count);
  883. temp_buflist = drm_realloc(dma->buflist,
  884. dma->buf_count * sizeof(*dma->buflist),
  885. (dma->buf_count + entry->buf_count)
  886. * sizeof(*dma->buflist), DRM_MEM_BUFS);
  887. if (!temp_buflist) {
  888. /* Free the entry because it isn't valid */
  889. drm_cleanup_buf_error(dev, entry);
  890. mutex_unlock(&dev->struct_mutex);
  891. atomic_dec(&dev->buf_alloc);
  892. return -ENOMEM;
  893. }
  894. dma->buflist = temp_buflist;
  895. for (i = 0; i < entry->buf_count; i++) {
  896. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  897. }
  898. dma->buf_count += entry->buf_count;
  899. dma->seg_count += entry->seg_count;
  900. dma->page_count += byte_count >> PAGE_SHIFT;
  901. dma->byte_count += byte_count;
  902. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  903. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  904. mutex_unlock(&dev->struct_mutex);
  905. request->count = entry->buf_count;
  906. request->size = size;
  907. dma->flags = _DRM_DMA_USE_SG;
  908. atomic_dec(&dev->buf_alloc);
  909. return 0;
  910. }
  911. static int drm_addbufs_fb(drm_device_t * dev, drm_buf_desc_t * request)
  912. {
  913. drm_device_dma_t *dma = dev->dma;
  914. drm_buf_entry_t *entry;
  915. drm_buf_t *buf;
  916. unsigned long offset;
  917. unsigned long agp_offset;
  918. int count;
  919. int order;
  920. int size;
  921. int alignment;
  922. int page_order;
  923. int total;
  924. int byte_count;
  925. int i;
  926. drm_buf_t **temp_buflist;
  927. if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
  928. return -EINVAL;
  929. if (!dma)
  930. return -EINVAL;
  931. if (!capable(CAP_SYS_ADMIN))
  932. return -EPERM;
  933. count = request->count;
  934. order = drm_order(request->size);
  935. size = 1 << order;
  936. alignment = (request->flags & _DRM_PAGE_ALIGN)
  937. ? PAGE_ALIGN(size) : size;
  938. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  939. total = PAGE_SIZE << page_order;
  940. byte_count = 0;
  941. agp_offset = request->agp_start;
  942. DRM_DEBUG("count: %d\n", count);
  943. DRM_DEBUG("order: %d\n", order);
  944. DRM_DEBUG("size: %d\n", size);
  945. DRM_DEBUG("agp_offset: %lu\n", agp_offset);
  946. DRM_DEBUG("alignment: %d\n", alignment);
  947. DRM_DEBUG("page_order: %d\n", page_order);
  948. DRM_DEBUG("total: %d\n", total);
  949. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  950. return -EINVAL;
  951. if (dev->queue_count)
  952. return -EBUSY; /* Not while in use */
  953. spin_lock(&dev->count_lock);
  954. if (dev->buf_use) {
  955. spin_unlock(&dev->count_lock);
  956. return -EBUSY;
  957. }
  958. atomic_inc(&dev->buf_alloc);
  959. spin_unlock(&dev->count_lock);
  960. mutex_lock(&dev->struct_mutex);
  961. entry = &dma->bufs[order];
  962. if (entry->buf_count) {
  963. mutex_unlock(&dev->struct_mutex);
  964. atomic_dec(&dev->buf_alloc);
  965. return -ENOMEM; /* May only call once for each order */
  966. }
  967. if (count < 0 || count > 4096) {
  968. mutex_unlock(&dev->struct_mutex);
  969. atomic_dec(&dev->buf_alloc);
  970. return -EINVAL;
  971. }
  972. entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
  973. DRM_MEM_BUFS);
  974. if (!entry->buflist) {
  975. mutex_unlock(&dev->struct_mutex);
  976. atomic_dec(&dev->buf_alloc);
  977. return -ENOMEM;
  978. }
  979. memset(entry->buflist, 0, count * sizeof(*entry->buflist));
  980. entry->buf_size = size;
  981. entry->page_order = page_order;
  982. offset = 0;
  983. while (entry->buf_count < count) {
  984. buf = &entry->buflist[entry->buf_count];
  985. buf->idx = dma->buf_count + entry->buf_count;
  986. buf->total = alignment;
  987. buf->order = order;
  988. buf->used = 0;
  989. buf->offset = (dma->byte_count + offset);
  990. buf->bus_address = agp_offset + offset;
  991. buf->address = (void *)(agp_offset + offset);
  992. buf->next = NULL;
  993. buf->waiting = 0;
  994. buf->pending = 0;
  995. init_waitqueue_head(&buf->dma_wait);
  996. buf->filp = NULL;
  997. buf->dev_priv_size = dev->driver->dev_priv_size;
  998. buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
  999. if (!buf->dev_private) {
  1000. /* Set count correctly so we free the proper amount. */
  1001. entry->buf_count = count;
  1002. drm_cleanup_buf_error(dev, entry);
  1003. mutex_unlock(&dev->struct_mutex);
  1004. atomic_dec(&dev->buf_alloc);
  1005. return -ENOMEM;
  1006. }
  1007. memset(buf->dev_private, 0, buf->dev_priv_size);
  1008. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  1009. offset += alignment;
  1010. entry->buf_count++;
  1011. byte_count += PAGE_SIZE << page_order;
  1012. }
  1013. DRM_DEBUG("byte_count: %d\n", byte_count);
  1014. temp_buflist = drm_realloc(dma->buflist,
  1015. dma->buf_count * sizeof(*dma->buflist),
  1016. (dma->buf_count + entry->buf_count)
  1017. * sizeof(*dma->buflist), DRM_MEM_BUFS);
  1018. if (!temp_buflist) {
  1019. /* Free the entry because it isn't valid */
  1020. drm_cleanup_buf_error(dev, entry);
  1021. mutex_unlock(&dev->struct_mutex);
  1022. atomic_dec(&dev->buf_alloc);
  1023. return -ENOMEM;
  1024. }
  1025. dma->buflist = temp_buflist;
  1026. for (i = 0; i < entry->buf_count; i++) {
  1027. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  1028. }
  1029. dma->buf_count += entry->buf_count;
  1030. dma->seg_count += entry->seg_count;
  1031. dma->page_count += byte_count >> PAGE_SHIFT;
  1032. dma->byte_count += byte_count;
  1033. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  1034. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  1035. mutex_unlock(&dev->struct_mutex);
  1036. request->count = entry->buf_count;
  1037. request->size = size;
  1038. dma->flags = _DRM_DMA_USE_FB;
  1039. atomic_dec(&dev->buf_alloc);
  1040. return 0;
  1041. }
  1042. /**
  1043. * Add buffers for DMA transfers (ioctl).
  1044. *
  1045. * \param inode device inode.
  1046. * \param filp file pointer.
  1047. * \param cmd command.
  1048. * \param arg pointer to a drm_buf_desc_t request.
  1049. * \return zero on success or a negative number on failure.
  1050. *
  1051. * According with the memory type specified in drm_buf_desc::flags and the
  1052. * build options, it dispatches the call either to addbufs_agp(),
  1053. * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
  1054. * PCI memory respectively.
  1055. */
  1056. int drm_addbufs(struct inode *inode, struct file *filp,
  1057. unsigned int cmd, unsigned long arg)
  1058. {
  1059. drm_buf_desc_t request;
  1060. drm_file_t *priv = filp->private_data;
  1061. drm_device_t *dev = priv->head->dev;
  1062. int ret;
  1063. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1064. return -EINVAL;
  1065. if (copy_from_user(&request, (drm_buf_desc_t __user *) arg,
  1066. sizeof(request)))
  1067. return -EFAULT;
  1068. #if __OS_HAS_AGP
  1069. if (request.flags & _DRM_AGP_BUFFER)
  1070. ret = drm_addbufs_agp(dev, &request);
  1071. else
  1072. #endif
  1073. if (request.flags & _DRM_SG_BUFFER)
  1074. ret = drm_addbufs_sg(dev, &request);
  1075. else if (request.flags & _DRM_FB_BUFFER)
  1076. ret = drm_addbufs_fb(dev, &request);
  1077. else
  1078. ret = drm_addbufs_pci(dev, &request);
  1079. if (ret == 0) {
  1080. if (copy_to_user((void __user *)arg, &request, sizeof(request))) {
  1081. ret = -EFAULT;
  1082. }
  1083. }
  1084. return ret;
  1085. }
  1086. /**
  1087. * Get information about the buffer mappings.
  1088. *
  1089. * This was originally mean for debugging purposes, or by a sophisticated
  1090. * client library to determine how best to use the available buffers (e.g.,
  1091. * large buffers can be used for image transfer).
  1092. *
  1093. * \param inode device inode.
  1094. * \param filp file pointer.
  1095. * \param cmd command.
  1096. * \param arg pointer to a drm_buf_info structure.
  1097. * \return zero on success or a negative number on failure.
  1098. *
  1099. * Increments drm_device::buf_use while holding the drm_device::count_lock
  1100. * lock, preventing of allocating more buffers after this call. Information
  1101. * about each requested buffer is then copied into user space.
  1102. */
  1103. int drm_infobufs(struct inode *inode, struct file *filp,
  1104. unsigned int cmd, unsigned long arg)
  1105. {
  1106. drm_file_t *priv = filp->private_data;
  1107. drm_device_t *dev = priv->head->dev;
  1108. drm_device_dma_t *dma = dev->dma;
  1109. drm_buf_info_t request;
  1110. drm_buf_info_t __user *argp = (void __user *)arg;
  1111. int i;
  1112. int count;
  1113. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1114. return -EINVAL;
  1115. if (!dma)
  1116. return -EINVAL;
  1117. spin_lock(&dev->count_lock);
  1118. if (atomic_read(&dev->buf_alloc)) {
  1119. spin_unlock(&dev->count_lock);
  1120. return -EBUSY;
  1121. }
  1122. ++dev->buf_use; /* Can't allocate more after this call */
  1123. spin_unlock(&dev->count_lock);
  1124. if (copy_from_user(&request, argp, sizeof(request)))
  1125. return -EFAULT;
  1126. for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
  1127. if (dma->bufs[i].buf_count)
  1128. ++count;
  1129. }
  1130. DRM_DEBUG("count = %d\n", count);
  1131. if (request.count >= count) {
  1132. for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
  1133. if (dma->bufs[i].buf_count) {
  1134. drm_buf_desc_t __user *to =
  1135. &request.list[count];
  1136. drm_buf_entry_t *from = &dma->bufs[i];
  1137. drm_freelist_t *list = &dma->bufs[i].freelist;
  1138. if (copy_to_user(&to->count,
  1139. &from->buf_count,
  1140. sizeof(from->buf_count)) ||
  1141. copy_to_user(&to->size,
  1142. &from->buf_size,
  1143. sizeof(from->buf_size)) ||
  1144. copy_to_user(&to->low_mark,
  1145. &list->low_mark,
  1146. sizeof(list->low_mark)) ||
  1147. copy_to_user(&to->high_mark,
  1148. &list->high_mark,
  1149. sizeof(list->high_mark)))
  1150. return -EFAULT;
  1151. DRM_DEBUG("%d %d %d %d %d\n",
  1152. i,
  1153. dma->bufs[i].buf_count,
  1154. dma->bufs[i].buf_size,
  1155. dma->bufs[i].freelist.low_mark,
  1156. dma->bufs[i].freelist.high_mark);
  1157. ++count;
  1158. }
  1159. }
  1160. }
  1161. request.count = count;
  1162. if (copy_to_user(argp, &request, sizeof(request)))
  1163. return -EFAULT;
  1164. return 0;
  1165. }
  1166. /**
  1167. * Specifies a low and high water mark for buffer allocation
  1168. *
  1169. * \param inode device inode.
  1170. * \param filp file pointer.
  1171. * \param cmd command.
  1172. * \param arg a pointer to a drm_buf_desc structure.
  1173. * \return zero on success or a negative number on failure.
  1174. *
  1175. * Verifies that the size order is bounded between the admissible orders and
  1176. * updates the respective drm_device_dma::bufs entry low and high water mark.
  1177. *
  1178. * \note This ioctl is deprecated and mostly never used.
  1179. */
  1180. int drm_markbufs(struct inode *inode, struct file *filp,
  1181. unsigned int cmd, unsigned long arg)
  1182. {
  1183. drm_file_t *priv = filp->private_data;
  1184. drm_device_t *dev = priv->head->dev;
  1185. drm_device_dma_t *dma = dev->dma;
  1186. drm_buf_desc_t request;
  1187. int order;
  1188. drm_buf_entry_t *entry;
  1189. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1190. return -EINVAL;
  1191. if (!dma)
  1192. return -EINVAL;
  1193. if (copy_from_user(&request,
  1194. (drm_buf_desc_t __user *) arg, sizeof(request)))
  1195. return -EFAULT;
  1196. DRM_DEBUG("%d, %d, %d\n",
  1197. request.size, request.low_mark, request.high_mark);
  1198. order = drm_order(request.size);
  1199. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  1200. return -EINVAL;
  1201. entry = &dma->bufs[order];
  1202. if (request.low_mark < 0 || request.low_mark > entry->buf_count)
  1203. return -EINVAL;
  1204. if (request.high_mark < 0 || request.high_mark > entry->buf_count)
  1205. return -EINVAL;
  1206. entry->freelist.low_mark = request.low_mark;
  1207. entry->freelist.high_mark = request.high_mark;
  1208. return 0;
  1209. }
  1210. /**
  1211. * Unreserve the buffers in list, previously reserved using drmDMA.
  1212. *
  1213. * \param inode device inode.
  1214. * \param filp file pointer.
  1215. * \param cmd command.
  1216. * \param arg pointer to a drm_buf_free structure.
  1217. * \return zero on success or a negative number on failure.
  1218. *
  1219. * Calls free_buffer() for each used buffer.
  1220. * This function is primarily used for debugging.
  1221. */
  1222. int drm_freebufs(struct inode *inode, struct file *filp,
  1223. unsigned int cmd, unsigned long arg)
  1224. {
  1225. drm_file_t *priv = filp->private_data;
  1226. drm_device_t *dev = priv->head->dev;
  1227. drm_device_dma_t *dma = dev->dma;
  1228. drm_buf_free_t request;
  1229. int i;
  1230. int idx;
  1231. drm_buf_t *buf;
  1232. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1233. return -EINVAL;
  1234. if (!dma)
  1235. return -EINVAL;
  1236. if (copy_from_user(&request,
  1237. (drm_buf_free_t __user *) arg, sizeof(request)))
  1238. return -EFAULT;
  1239. DRM_DEBUG("%d\n", request.count);
  1240. for (i = 0; i < request.count; i++) {
  1241. if (copy_from_user(&idx, &request.list[i], sizeof(idx)))
  1242. return -EFAULT;
  1243. if (idx < 0 || idx >= dma->buf_count) {
  1244. DRM_ERROR("Index %d (of %d max)\n",
  1245. idx, dma->buf_count - 1);
  1246. return -EINVAL;
  1247. }
  1248. buf = dma->buflist[idx];
  1249. if (buf->filp != filp) {
  1250. DRM_ERROR("Process %d freeing buffer not owned\n",
  1251. current->pid);
  1252. return -EINVAL;
  1253. }
  1254. drm_free_buffer(dev, buf);
  1255. }
  1256. return 0;
  1257. }
  1258. /**
  1259. * Maps all of the DMA buffers into client-virtual space (ioctl).
  1260. *
  1261. * \param inode device inode.
  1262. * \param filp file pointer.
  1263. * \param cmd command.
  1264. * \param arg pointer to a drm_buf_map structure.
  1265. * \return zero on success or a negative number on failure.
  1266. *
  1267. * Maps the AGP or SG buffer region with do_mmap(), and copies information
  1268. * about each buffer into user space. The PCI buffers are already mapped on the
  1269. * addbufs_pci() call.
  1270. */
  1271. int drm_mapbufs(struct inode *inode, struct file *filp,
  1272. unsigned int cmd, unsigned long arg)
  1273. {
  1274. drm_file_t *priv = filp->private_data;
  1275. drm_device_t *dev = priv->head->dev;
  1276. drm_device_dma_t *dma = dev->dma;
  1277. drm_buf_map_t __user *argp = (void __user *)arg;
  1278. int retcode = 0;
  1279. const int zero = 0;
  1280. unsigned long virtual;
  1281. unsigned long address;
  1282. drm_buf_map_t request;
  1283. int i;
  1284. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1285. return -EINVAL;
  1286. if (!dma)
  1287. return -EINVAL;
  1288. spin_lock(&dev->count_lock);
  1289. if (atomic_read(&dev->buf_alloc)) {
  1290. spin_unlock(&dev->count_lock);
  1291. return -EBUSY;
  1292. }
  1293. dev->buf_use++; /* Can't allocate more after this call */
  1294. spin_unlock(&dev->count_lock);
  1295. if (copy_from_user(&request, argp, sizeof(request)))
  1296. return -EFAULT;
  1297. if (request.count >= dma->buf_count) {
  1298. if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
  1299. || (drm_core_check_feature(dev, DRIVER_SG)
  1300. && (dma->flags & _DRM_DMA_USE_SG))
  1301. || (drm_core_check_feature(dev, DRIVER_FB_DMA)
  1302. && (dma->flags & _DRM_DMA_USE_FB))) {
  1303. drm_map_t *map = dev->agp_buffer_map;
  1304. unsigned long token = dev->agp_buffer_token;
  1305. if (!map) {
  1306. retcode = -EINVAL;
  1307. goto done;
  1308. }
  1309. down_write(&current->mm->mmap_sem);
  1310. virtual = do_mmap(filp, 0, map->size,
  1311. PROT_READ | PROT_WRITE,
  1312. MAP_SHARED, token);
  1313. up_write(&current->mm->mmap_sem);
  1314. } else {
  1315. down_write(&current->mm->mmap_sem);
  1316. virtual = do_mmap(filp, 0, dma->byte_count,
  1317. PROT_READ | PROT_WRITE,
  1318. MAP_SHARED, 0);
  1319. up_write(&current->mm->mmap_sem);
  1320. }
  1321. if (virtual > -1024UL) {
  1322. /* Real error */
  1323. retcode = (signed long)virtual;
  1324. goto done;
  1325. }
  1326. request.virtual = (void __user *)virtual;
  1327. for (i = 0; i < dma->buf_count; i++) {
  1328. if (copy_to_user(&request.list[i].idx,
  1329. &dma->buflist[i]->idx,
  1330. sizeof(request.list[0].idx))) {
  1331. retcode = -EFAULT;
  1332. goto done;
  1333. }
  1334. if (copy_to_user(&request.list[i].total,
  1335. &dma->buflist[i]->total,
  1336. sizeof(request.list[0].total))) {
  1337. retcode = -EFAULT;
  1338. goto done;
  1339. }
  1340. if (copy_to_user(&request.list[i].used,
  1341. &zero, sizeof(zero))) {
  1342. retcode = -EFAULT;
  1343. goto done;
  1344. }
  1345. address = virtual + dma->buflist[i]->offset; /* *** */
  1346. if (copy_to_user(&request.list[i].address,
  1347. &address, sizeof(address))) {
  1348. retcode = -EFAULT;
  1349. goto done;
  1350. }
  1351. }
  1352. }
  1353. done:
  1354. request.count = dma->buf_count;
  1355. DRM_DEBUG("%d buffers, retcode = %d\n", request.count, retcode);
  1356. if (copy_to_user(argp, &request, sizeof(request)))
  1357. return -EFAULT;
  1358. return retcode;
  1359. }
  1360. /**
  1361. * Compute size order. Returns the exponent of the smaller power of two which
  1362. * is greater or equal to given number.
  1363. *
  1364. * \param size size.
  1365. * \return order.
  1366. *
  1367. * \todo Can be made faster.
  1368. */
  1369. int drm_order(unsigned long size)
  1370. {
  1371. int order;
  1372. unsigned long tmp;
  1373. for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
  1374. if (size & (size - 1))
  1375. ++order;
  1376. return order;
  1377. }
  1378. EXPORT_SYMBOL(drm_order);