drm_bufs.c 43 KB

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