drm_bufs.c 42 KB

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