drm_bufs.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  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 <drm/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 (map->type == _DRM_FRAME_BUFFER ||
  192. (map->flags & _DRM_WRITE_COMBINING)) {
  193. map->mtrr =
  194. arch_phys_wc_add(map->offset, map->size);
  195. }
  196. if (map->type == _DRM_REGISTERS) {
  197. if (map->flags & _DRM_WRITE_COMBINING)
  198. map->handle = ioremap_wc(map->offset,
  199. map->size);
  200. else
  201. map->handle = ioremap(map->offset, map->size);
  202. if (!map->handle) {
  203. kfree(map);
  204. return -ENOMEM;
  205. }
  206. }
  207. break;
  208. case _DRM_SHM:
  209. list = drm_find_matching_map(dev, map);
  210. if (list != NULL) {
  211. if(list->map->size != map->size) {
  212. DRM_DEBUG("Matching maps of type %d with "
  213. "mismatched sizes, (%ld vs %ld)\n",
  214. map->type, map->size, list->map->size);
  215. list->map->size = map->size;
  216. }
  217. kfree(map);
  218. *maplist = list;
  219. return 0;
  220. }
  221. map->handle = vmalloc_user(map->size);
  222. DRM_DEBUG("%lu %d %p\n",
  223. map->size, order_base_2(map->size), map->handle);
  224. if (!map->handle) {
  225. kfree(map);
  226. return -ENOMEM;
  227. }
  228. map->offset = (unsigned long)map->handle;
  229. if (map->flags & _DRM_CONTAINS_LOCK) {
  230. /* Prevent a 2nd X Server from creating a 2nd lock */
  231. if (dev->primary->master->lock.hw_lock != NULL) {
  232. vfree(map->handle);
  233. kfree(map);
  234. return -EBUSY;
  235. }
  236. dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle; /* Pointer to lock */
  237. }
  238. break;
  239. case _DRM_AGP: {
  240. struct drm_agp_mem *entry;
  241. int valid = 0;
  242. if (!dev->agp) {
  243. kfree(map);
  244. return -EINVAL;
  245. }
  246. #ifdef __alpha__
  247. map->offset += dev->hose->mem_space->start;
  248. #endif
  249. /* In some cases (i810 driver), user space may have already
  250. * added the AGP base itself, because dev->agp->base previously
  251. * only got set during AGP enable. So, only add the base
  252. * address if the map's offset isn't already within the
  253. * aperture.
  254. */
  255. if (map->offset < dev->agp->base ||
  256. map->offset > dev->agp->base +
  257. dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
  258. map->offset += dev->agp->base;
  259. }
  260. map->mtrr = dev->agp->agp_mtrr; /* for getmap */
  261. /* This assumes the DRM is in total control of AGP space.
  262. * It's not always the case as AGP can be in the control
  263. * of user space (i.e. i810 driver). So this loop will get
  264. * skipped and we double check that dev->agp->memory is
  265. * actually set as well as being invalid before EPERM'ing
  266. */
  267. list_for_each_entry(entry, &dev->agp->memory, head) {
  268. if ((map->offset >= entry->bound) &&
  269. (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
  270. valid = 1;
  271. break;
  272. }
  273. }
  274. if (!list_empty(&dev->agp->memory) && !valid) {
  275. kfree(map);
  276. return -EPERM;
  277. }
  278. DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
  279. (unsigned long long)map->offset, map->size);
  280. break;
  281. }
  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. /*
  376. * It appears that there are no users of this value whatsoever --
  377. * drmAddMap just discards it. Let's not encourage its use.
  378. * (Keeping drm_addmap_core's returned mtrr value would be wrong --
  379. * it's not a real mtrr index anymore.)
  380. */
  381. map->mtrr = -1;
  382. return 0;
  383. }
  384. /**
  385. * Remove a map private from list and deallocate resources if the mapping
  386. * isn't in use.
  387. *
  388. * Searches the map on drm_device::maplist, removes it from the list, see if
  389. * its being used, and free any associate resource (such as MTRR's) if it's not
  390. * being on use.
  391. *
  392. * \sa drm_addmap
  393. */
  394. int drm_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
  395. {
  396. struct drm_map_list *r_list = NULL, *list_t;
  397. drm_dma_handle_t dmah;
  398. int found = 0;
  399. struct drm_master *master;
  400. /* Find the list entry for the map and remove it */
  401. list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
  402. if (r_list->map == map) {
  403. master = r_list->master;
  404. list_del(&r_list->head);
  405. drm_ht_remove_key(&dev->map_hash,
  406. r_list->user_token >> PAGE_SHIFT);
  407. kfree(r_list);
  408. found = 1;
  409. break;
  410. }
  411. }
  412. if (!found)
  413. return -EINVAL;
  414. switch (map->type) {
  415. case _DRM_REGISTERS:
  416. iounmap(map->handle);
  417. /* FALLTHROUGH */
  418. case _DRM_FRAME_BUFFER:
  419. arch_phys_wc_del(map->mtrr);
  420. break;
  421. case _DRM_SHM:
  422. vfree(map->handle);
  423. if (master) {
  424. if (dev->sigdata.lock == master->lock.hw_lock)
  425. dev->sigdata.lock = NULL;
  426. master->lock.hw_lock = NULL; /* SHM removed */
  427. master->lock.file_priv = NULL;
  428. wake_up_interruptible_all(&master->lock.lock_queue);
  429. }
  430. break;
  431. case _DRM_AGP:
  432. case _DRM_SCATTER_GATHER:
  433. break;
  434. case _DRM_CONSISTENT:
  435. dmah.vaddr = map->handle;
  436. dmah.busaddr = map->offset;
  437. dmah.size = map->size;
  438. __drm_pci_free(dev, &dmah);
  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 = order_base_2(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. /* Make sure buffers are located in AGP memory that we own */
  580. valid = 0;
  581. list_for_each_entry(agp_entry, &dev->agp->memory, head) {
  582. if ((agp_offset >= agp_entry->bound) &&
  583. (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
  584. valid = 1;
  585. break;
  586. }
  587. }
  588. if (!list_empty(&dev->agp->memory) && !valid) {
  589. DRM_DEBUG("zone invalid\n");
  590. return -EINVAL;
  591. }
  592. spin_lock(&dev->buf_lock);
  593. if (dev->buf_use) {
  594. spin_unlock(&dev->buf_lock);
  595. return -EBUSY;
  596. }
  597. atomic_inc(&dev->buf_alloc);
  598. spin_unlock(&dev->buf_lock);
  599. mutex_lock(&dev->struct_mutex);
  600. entry = &dma->bufs[order];
  601. if (entry->buf_count) {
  602. mutex_unlock(&dev->struct_mutex);
  603. atomic_dec(&dev->buf_alloc);
  604. return -ENOMEM; /* May only call once for each order */
  605. }
  606. if (count < 0 || count > 4096) {
  607. mutex_unlock(&dev->struct_mutex);
  608. atomic_dec(&dev->buf_alloc);
  609. return -EINVAL;
  610. }
  611. entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
  612. if (!entry->buflist) {
  613. mutex_unlock(&dev->struct_mutex);
  614. atomic_dec(&dev->buf_alloc);
  615. return -ENOMEM;
  616. }
  617. entry->buf_size = size;
  618. entry->page_order = page_order;
  619. offset = 0;
  620. while (entry->buf_count < count) {
  621. buf = &entry->buflist[entry->buf_count];
  622. buf->idx = dma->buf_count + entry->buf_count;
  623. buf->total = alignment;
  624. buf->order = order;
  625. buf->used = 0;
  626. buf->offset = (dma->byte_count + offset);
  627. buf->bus_address = agp_offset + offset;
  628. buf->address = (void *)(agp_offset + offset);
  629. buf->next = NULL;
  630. buf->waiting = 0;
  631. buf->pending = 0;
  632. buf->file_priv = NULL;
  633. buf->dev_priv_size = dev->driver->dev_priv_size;
  634. buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
  635. if (!buf->dev_private) {
  636. /* Set count correctly so we free the proper amount. */
  637. entry->buf_count = count;
  638. drm_cleanup_buf_error(dev, entry);
  639. mutex_unlock(&dev->struct_mutex);
  640. atomic_dec(&dev->buf_alloc);
  641. return -ENOMEM;
  642. }
  643. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  644. offset += alignment;
  645. entry->buf_count++;
  646. byte_count += PAGE_SIZE << page_order;
  647. }
  648. DRM_DEBUG("byte_count: %d\n", byte_count);
  649. temp_buflist = krealloc(dma->buflist,
  650. (dma->buf_count + entry->buf_count) *
  651. sizeof(*dma->buflist), GFP_KERNEL);
  652. if (!temp_buflist) {
  653. /* Free the entry because it isn't valid */
  654. drm_cleanup_buf_error(dev, entry);
  655. mutex_unlock(&dev->struct_mutex);
  656. atomic_dec(&dev->buf_alloc);
  657. return -ENOMEM;
  658. }
  659. dma->buflist = temp_buflist;
  660. for (i = 0; i < entry->buf_count; i++) {
  661. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  662. }
  663. dma->buf_count += entry->buf_count;
  664. dma->seg_count += entry->seg_count;
  665. dma->page_count += byte_count >> PAGE_SHIFT;
  666. dma->byte_count += byte_count;
  667. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  668. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  669. mutex_unlock(&dev->struct_mutex);
  670. request->count = entry->buf_count;
  671. request->size = size;
  672. dma->flags = _DRM_DMA_USE_AGP;
  673. atomic_dec(&dev->buf_alloc);
  674. return 0;
  675. }
  676. EXPORT_SYMBOL(drm_addbufs_agp);
  677. #endif /* __OS_HAS_AGP */
  678. int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
  679. {
  680. struct drm_device_dma *dma = dev->dma;
  681. int count;
  682. int order;
  683. int size;
  684. int total;
  685. int page_order;
  686. struct drm_buf_entry *entry;
  687. drm_dma_handle_t *dmah;
  688. struct drm_buf *buf;
  689. int alignment;
  690. unsigned long offset;
  691. int i;
  692. int byte_count;
  693. int page_count;
  694. unsigned long *temp_pagelist;
  695. struct drm_buf **temp_buflist;
  696. if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
  697. return -EINVAL;
  698. if (!dma)
  699. return -EINVAL;
  700. if (!capable(CAP_SYS_ADMIN))
  701. return -EPERM;
  702. count = request->count;
  703. order = order_base_2(request->size);
  704. size = 1 << order;
  705. DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
  706. request->count, request->size, size, order);
  707. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  708. return -EINVAL;
  709. alignment = (request->flags & _DRM_PAGE_ALIGN)
  710. ? PAGE_ALIGN(size) : size;
  711. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  712. total = PAGE_SIZE << page_order;
  713. spin_lock(&dev->buf_lock);
  714. if (dev->buf_use) {
  715. spin_unlock(&dev->buf_lock);
  716. return -EBUSY;
  717. }
  718. atomic_inc(&dev->buf_alloc);
  719. spin_unlock(&dev->buf_lock);
  720. mutex_lock(&dev->struct_mutex);
  721. entry = &dma->bufs[order];
  722. if (entry->buf_count) {
  723. mutex_unlock(&dev->struct_mutex);
  724. atomic_dec(&dev->buf_alloc);
  725. return -ENOMEM; /* May only call once for each order */
  726. }
  727. if (count < 0 || count > 4096) {
  728. mutex_unlock(&dev->struct_mutex);
  729. atomic_dec(&dev->buf_alloc);
  730. return -EINVAL;
  731. }
  732. entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
  733. if (!entry->buflist) {
  734. mutex_unlock(&dev->struct_mutex);
  735. atomic_dec(&dev->buf_alloc);
  736. return -ENOMEM;
  737. }
  738. entry->seglist = kzalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
  739. if (!entry->seglist) {
  740. kfree(entry->buflist);
  741. mutex_unlock(&dev->struct_mutex);
  742. atomic_dec(&dev->buf_alloc);
  743. return -ENOMEM;
  744. }
  745. /* Keep the original pagelist until we know all the allocations
  746. * have succeeded
  747. */
  748. temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
  749. sizeof(*dma->pagelist), GFP_KERNEL);
  750. if (!temp_pagelist) {
  751. kfree(entry->buflist);
  752. kfree(entry->seglist);
  753. mutex_unlock(&dev->struct_mutex);
  754. atomic_dec(&dev->buf_alloc);
  755. return -ENOMEM;
  756. }
  757. memcpy(temp_pagelist,
  758. dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
  759. DRM_DEBUG("pagelist: %d entries\n",
  760. dma->page_count + (count << page_order));
  761. entry->buf_size = size;
  762. entry->page_order = page_order;
  763. byte_count = 0;
  764. page_count = 0;
  765. while (entry->buf_count < count) {
  766. dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
  767. if (!dmah) {
  768. /* Set count correctly so we free the proper amount. */
  769. entry->buf_count = count;
  770. entry->seg_count = count;
  771. drm_cleanup_buf_error(dev, entry);
  772. kfree(temp_pagelist);
  773. mutex_unlock(&dev->struct_mutex);
  774. atomic_dec(&dev->buf_alloc);
  775. return -ENOMEM;
  776. }
  777. entry->seglist[entry->seg_count++] = dmah;
  778. for (i = 0; i < (1 << page_order); i++) {
  779. DRM_DEBUG("page %d @ 0x%08lx\n",
  780. dma->page_count + page_count,
  781. (unsigned long)dmah->vaddr + PAGE_SIZE * i);
  782. temp_pagelist[dma->page_count + page_count++]
  783. = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
  784. }
  785. for (offset = 0;
  786. offset + size <= total && entry->buf_count < count;
  787. offset += alignment, ++entry->buf_count) {
  788. buf = &entry->buflist[entry->buf_count];
  789. buf->idx = dma->buf_count + entry->buf_count;
  790. buf->total = alignment;
  791. buf->order = order;
  792. buf->used = 0;
  793. buf->offset = (dma->byte_count + byte_count + offset);
  794. buf->address = (void *)(dmah->vaddr + offset);
  795. buf->bus_address = dmah->busaddr + offset;
  796. buf->next = NULL;
  797. buf->waiting = 0;
  798. buf->pending = 0;
  799. buf->file_priv = NULL;
  800. buf->dev_priv_size = dev->driver->dev_priv_size;
  801. buf->dev_private = kzalloc(buf->dev_priv_size,
  802. GFP_KERNEL);
  803. if (!buf->dev_private) {
  804. /* Set count correctly so we free the proper amount. */
  805. entry->buf_count = count;
  806. entry->seg_count = count;
  807. drm_cleanup_buf_error(dev, entry);
  808. kfree(temp_pagelist);
  809. mutex_unlock(&dev->struct_mutex);
  810. atomic_dec(&dev->buf_alloc);
  811. return -ENOMEM;
  812. }
  813. DRM_DEBUG("buffer %d @ %p\n",
  814. entry->buf_count, buf->address);
  815. }
  816. byte_count += PAGE_SIZE << page_order;
  817. }
  818. temp_buflist = krealloc(dma->buflist,
  819. (dma->buf_count + entry->buf_count) *
  820. sizeof(*dma->buflist), GFP_KERNEL);
  821. if (!temp_buflist) {
  822. /* Free the entry because it isn't valid */
  823. drm_cleanup_buf_error(dev, entry);
  824. kfree(temp_pagelist);
  825. mutex_unlock(&dev->struct_mutex);
  826. atomic_dec(&dev->buf_alloc);
  827. return -ENOMEM;
  828. }
  829. dma->buflist = temp_buflist;
  830. for (i = 0; i < entry->buf_count; i++) {
  831. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  832. }
  833. /* No allocations failed, so now we can replace the original pagelist
  834. * with the new one.
  835. */
  836. if (dma->page_count) {
  837. kfree(dma->pagelist);
  838. }
  839. dma->pagelist = temp_pagelist;
  840. dma->buf_count += entry->buf_count;
  841. dma->seg_count += entry->seg_count;
  842. dma->page_count += entry->seg_count << page_order;
  843. dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
  844. mutex_unlock(&dev->struct_mutex);
  845. request->count = entry->buf_count;
  846. request->size = size;
  847. if (request->flags & _DRM_PCI_BUFFER_RO)
  848. dma->flags = _DRM_DMA_USE_PCI_RO;
  849. atomic_dec(&dev->buf_alloc);
  850. return 0;
  851. }
  852. EXPORT_SYMBOL(drm_addbufs_pci);
  853. static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
  854. {
  855. struct drm_device_dma *dma = dev->dma;
  856. struct drm_buf_entry *entry;
  857. struct drm_buf *buf;
  858. unsigned long offset;
  859. unsigned long agp_offset;
  860. int count;
  861. int order;
  862. int size;
  863. int alignment;
  864. int page_order;
  865. int total;
  866. int byte_count;
  867. int i;
  868. struct drm_buf **temp_buflist;
  869. if (!drm_core_check_feature(dev, DRIVER_SG))
  870. return -EINVAL;
  871. if (!dma)
  872. return -EINVAL;
  873. if (!capable(CAP_SYS_ADMIN))
  874. return -EPERM;
  875. count = request->count;
  876. order = order_base_2(request->size);
  877. size = 1 << order;
  878. alignment = (request->flags & _DRM_PAGE_ALIGN)
  879. ? PAGE_ALIGN(size) : size;
  880. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  881. total = PAGE_SIZE << page_order;
  882. byte_count = 0;
  883. agp_offset = request->agp_start;
  884. DRM_DEBUG("count: %d\n", count);
  885. DRM_DEBUG("order: %d\n", order);
  886. DRM_DEBUG("size: %d\n", size);
  887. DRM_DEBUG("agp_offset: %lu\n", agp_offset);
  888. DRM_DEBUG("alignment: %d\n", alignment);
  889. DRM_DEBUG("page_order: %d\n", page_order);
  890. DRM_DEBUG("total: %d\n", total);
  891. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  892. return -EINVAL;
  893. spin_lock(&dev->buf_lock);
  894. if (dev->buf_use) {
  895. spin_unlock(&dev->buf_lock);
  896. return -EBUSY;
  897. }
  898. atomic_inc(&dev->buf_alloc);
  899. spin_unlock(&dev->buf_lock);
  900. mutex_lock(&dev->struct_mutex);
  901. entry = &dma->bufs[order];
  902. if (entry->buf_count) {
  903. mutex_unlock(&dev->struct_mutex);
  904. atomic_dec(&dev->buf_alloc);
  905. return -ENOMEM; /* May only call once for each order */
  906. }
  907. if (count < 0 || count > 4096) {
  908. mutex_unlock(&dev->struct_mutex);
  909. atomic_dec(&dev->buf_alloc);
  910. return -EINVAL;
  911. }
  912. entry->buflist = kzalloc(count * sizeof(*entry->buflist),
  913. GFP_KERNEL);
  914. if (!entry->buflist) {
  915. mutex_unlock(&dev->struct_mutex);
  916. atomic_dec(&dev->buf_alloc);
  917. return -ENOMEM;
  918. }
  919. entry->buf_size = size;
  920. entry->page_order = page_order;
  921. offset = 0;
  922. while (entry->buf_count < count) {
  923. buf = &entry->buflist[entry->buf_count];
  924. buf->idx = dma->buf_count + entry->buf_count;
  925. buf->total = alignment;
  926. buf->order = order;
  927. buf->used = 0;
  928. buf->offset = (dma->byte_count + offset);
  929. buf->bus_address = agp_offset + offset;
  930. buf->address = (void *)(agp_offset + offset
  931. + (unsigned long)dev->sg->virtual);
  932. buf->next = NULL;
  933. buf->waiting = 0;
  934. buf->pending = 0;
  935. buf->file_priv = NULL;
  936. buf->dev_priv_size = dev->driver->dev_priv_size;
  937. buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
  938. if (!buf->dev_private) {
  939. /* Set count correctly so we free the proper amount. */
  940. entry->buf_count = count;
  941. drm_cleanup_buf_error(dev, entry);
  942. mutex_unlock(&dev->struct_mutex);
  943. atomic_dec(&dev->buf_alloc);
  944. return -ENOMEM;
  945. }
  946. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  947. offset += alignment;
  948. entry->buf_count++;
  949. byte_count += PAGE_SIZE << page_order;
  950. }
  951. DRM_DEBUG("byte_count: %d\n", byte_count);
  952. temp_buflist = krealloc(dma->buflist,
  953. (dma->buf_count + entry->buf_count) *
  954. sizeof(*dma->buflist), GFP_KERNEL);
  955. if (!temp_buflist) {
  956. /* Free the entry because it isn't valid */
  957. drm_cleanup_buf_error(dev, entry);
  958. mutex_unlock(&dev->struct_mutex);
  959. atomic_dec(&dev->buf_alloc);
  960. return -ENOMEM;
  961. }
  962. dma->buflist = temp_buflist;
  963. for (i = 0; i < entry->buf_count; i++) {
  964. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  965. }
  966. dma->buf_count += entry->buf_count;
  967. dma->seg_count += entry->seg_count;
  968. dma->page_count += byte_count >> PAGE_SHIFT;
  969. dma->byte_count += byte_count;
  970. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  971. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  972. mutex_unlock(&dev->struct_mutex);
  973. request->count = entry->buf_count;
  974. request->size = size;
  975. dma->flags = _DRM_DMA_USE_SG;
  976. atomic_dec(&dev->buf_alloc);
  977. return 0;
  978. }
  979. /**
  980. * Add buffers for DMA transfers (ioctl).
  981. *
  982. * \param inode device inode.
  983. * \param file_priv DRM file private.
  984. * \param cmd command.
  985. * \param arg pointer to a struct drm_buf_desc request.
  986. * \return zero on success or a negative number on failure.
  987. *
  988. * According with the memory type specified in drm_buf_desc::flags and the
  989. * build options, it dispatches the call either to addbufs_agp(),
  990. * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
  991. * PCI memory respectively.
  992. */
  993. int drm_addbufs(struct drm_device *dev, void *data,
  994. struct drm_file *file_priv)
  995. {
  996. struct drm_buf_desc *request = data;
  997. int ret;
  998. if (drm_core_check_feature(dev, DRIVER_MODESET))
  999. return -EINVAL;
  1000. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1001. return -EINVAL;
  1002. #if __OS_HAS_AGP
  1003. if (request->flags & _DRM_AGP_BUFFER)
  1004. ret = drm_addbufs_agp(dev, request);
  1005. else
  1006. #endif
  1007. if (request->flags & _DRM_SG_BUFFER)
  1008. ret = drm_addbufs_sg(dev, request);
  1009. else if (request->flags & _DRM_FB_BUFFER)
  1010. ret = -EINVAL;
  1011. else
  1012. ret = drm_addbufs_pci(dev, request);
  1013. return ret;
  1014. }
  1015. /**
  1016. * Get information about the buffer mappings.
  1017. *
  1018. * This was originally mean for debugging purposes, or by a sophisticated
  1019. * client library to determine how best to use the available buffers (e.g.,
  1020. * large buffers can be used for image transfer).
  1021. *
  1022. * \param inode device inode.
  1023. * \param file_priv DRM file private.
  1024. * \param cmd command.
  1025. * \param arg pointer to a drm_buf_info structure.
  1026. * \return zero on success or a negative number on failure.
  1027. *
  1028. * Increments drm_device::buf_use while holding the drm_device::buf_lock
  1029. * lock, preventing of allocating more buffers after this call. Information
  1030. * about each requested buffer is then copied into user space.
  1031. */
  1032. int drm_infobufs(struct drm_device *dev, void *data,
  1033. struct drm_file *file_priv)
  1034. {
  1035. struct drm_device_dma *dma = dev->dma;
  1036. struct drm_buf_info *request = data;
  1037. int i;
  1038. int count;
  1039. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1040. return -EINVAL;
  1041. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1042. return -EINVAL;
  1043. if (!dma)
  1044. return -EINVAL;
  1045. spin_lock(&dev->buf_lock);
  1046. if (atomic_read(&dev->buf_alloc)) {
  1047. spin_unlock(&dev->buf_lock);
  1048. return -EBUSY;
  1049. }
  1050. ++dev->buf_use; /* Can't allocate more after this call */
  1051. spin_unlock(&dev->buf_lock);
  1052. for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
  1053. if (dma->bufs[i].buf_count)
  1054. ++count;
  1055. }
  1056. DRM_DEBUG("count = %d\n", count);
  1057. if (request->count >= count) {
  1058. for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
  1059. if (dma->bufs[i].buf_count) {
  1060. struct drm_buf_desc __user *to =
  1061. &request->list[count];
  1062. struct drm_buf_entry *from = &dma->bufs[i];
  1063. if (copy_to_user(&to->count,
  1064. &from->buf_count,
  1065. sizeof(from->buf_count)) ||
  1066. copy_to_user(&to->size,
  1067. &from->buf_size,
  1068. sizeof(from->buf_size)) ||
  1069. copy_to_user(&to->low_mark,
  1070. &from->low_mark,
  1071. sizeof(from->low_mark)) ||
  1072. copy_to_user(&to->high_mark,
  1073. &from->high_mark,
  1074. sizeof(from->high_mark)))
  1075. return -EFAULT;
  1076. DRM_DEBUG("%d %d %d %d %d\n",
  1077. i,
  1078. dma->bufs[i].buf_count,
  1079. dma->bufs[i].buf_size,
  1080. dma->bufs[i].low_mark,
  1081. dma->bufs[i].high_mark);
  1082. ++count;
  1083. }
  1084. }
  1085. }
  1086. request->count = count;
  1087. return 0;
  1088. }
  1089. /**
  1090. * Specifies a low and high water mark for buffer allocation
  1091. *
  1092. * \param inode device inode.
  1093. * \param file_priv DRM file private.
  1094. * \param cmd command.
  1095. * \param arg a pointer to a drm_buf_desc structure.
  1096. * \return zero on success or a negative number on failure.
  1097. *
  1098. * Verifies that the size order is bounded between the admissible orders and
  1099. * updates the respective drm_device_dma::bufs entry low and high water mark.
  1100. *
  1101. * \note This ioctl is deprecated and mostly never used.
  1102. */
  1103. int drm_markbufs(struct drm_device *dev, void *data,
  1104. struct drm_file *file_priv)
  1105. {
  1106. struct drm_device_dma *dma = dev->dma;
  1107. struct drm_buf_desc *request = data;
  1108. int order;
  1109. struct drm_buf_entry *entry;
  1110. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1111. return -EINVAL;
  1112. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1113. return -EINVAL;
  1114. if (!dma)
  1115. return -EINVAL;
  1116. DRM_DEBUG("%d, %d, %d\n",
  1117. request->size, request->low_mark, request->high_mark);
  1118. order = order_base_2(request->size);
  1119. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  1120. return -EINVAL;
  1121. entry = &dma->bufs[order];
  1122. if (request->low_mark < 0 || request->low_mark > entry->buf_count)
  1123. return -EINVAL;
  1124. if (request->high_mark < 0 || request->high_mark > entry->buf_count)
  1125. return -EINVAL;
  1126. entry->low_mark = request->low_mark;
  1127. entry->high_mark = request->high_mark;
  1128. return 0;
  1129. }
  1130. /**
  1131. * Unreserve the buffers in list, previously reserved using drmDMA.
  1132. *
  1133. * \param inode device inode.
  1134. * \param file_priv DRM file private.
  1135. * \param cmd command.
  1136. * \param arg pointer to a drm_buf_free structure.
  1137. * \return zero on success or a negative number on failure.
  1138. *
  1139. * Calls free_buffer() for each used buffer.
  1140. * This function is primarily used for debugging.
  1141. */
  1142. int drm_freebufs(struct drm_device *dev, void *data,
  1143. struct drm_file *file_priv)
  1144. {
  1145. struct drm_device_dma *dma = dev->dma;
  1146. struct drm_buf_free *request = data;
  1147. int i;
  1148. int idx;
  1149. struct drm_buf *buf;
  1150. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1151. return -EINVAL;
  1152. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1153. return -EINVAL;
  1154. if (!dma)
  1155. return -EINVAL;
  1156. DRM_DEBUG("%d\n", request->count);
  1157. for (i = 0; i < request->count; i++) {
  1158. if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
  1159. return -EFAULT;
  1160. if (idx < 0 || idx >= dma->buf_count) {
  1161. DRM_ERROR("Index %d (of %d max)\n",
  1162. idx, dma->buf_count - 1);
  1163. return -EINVAL;
  1164. }
  1165. buf = dma->buflist[idx];
  1166. if (buf->file_priv != file_priv) {
  1167. DRM_ERROR("Process %d freeing buffer not owned\n",
  1168. task_pid_nr(current));
  1169. return -EINVAL;
  1170. }
  1171. drm_free_buffer(dev, buf);
  1172. }
  1173. return 0;
  1174. }
  1175. /**
  1176. * Maps all of the DMA buffers into client-virtual space (ioctl).
  1177. *
  1178. * \param inode device inode.
  1179. * \param file_priv DRM file private.
  1180. * \param cmd command.
  1181. * \param arg pointer to a drm_buf_map structure.
  1182. * \return zero on success or a negative number on failure.
  1183. *
  1184. * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
  1185. * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
  1186. * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
  1187. * drm_mmap_dma().
  1188. */
  1189. int drm_mapbufs(struct drm_device *dev, void *data,
  1190. struct drm_file *file_priv)
  1191. {
  1192. struct drm_device_dma *dma = dev->dma;
  1193. int retcode = 0;
  1194. const int zero = 0;
  1195. unsigned long virtual;
  1196. unsigned long address;
  1197. struct drm_buf_map *request = data;
  1198. int i;
  1199. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1200. return -EINVAL;
  1201. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1202. return -EINVAL;
  1203. if (!dma)
  1204. return -EINVAL;
  1205. spin_lock(&dev->buf_lock);
  1206. if (atomic_read(&dev->buf_alloc)) {
  1207. spin_unlock(&dev->buf_lock);
  1208. return -EBUSY;
  1209. }
  1210. dev->buf_use++; /* Can't allocate more after this call */
  1211. spin_unlock(&dev->buf_lock);
  1212. if (request->count >= dma->buf_count) {
  1213. if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP))
  1214. || (drm_core_check_feature(dev, DRIVER_SG)
  1215. && (dma->flags & _DRM_DMA_USE_SG))) {
  1216. struct drm_local_map *map = dev->agp_buffer_map;
  1217. unsigned long token = dev->agp_buffer_token;
  1218. if (!map) {
  1219. retcode = -EINVAL;
  1220. goto done;
  1221. }
  1222. virtual = vm_mmap(file_priv->filp, 0, map->size,
  1223. PROT_READ | PROT_WRITE,
  1224. MAP_SHARED,
  1225. token);
  1226. } else {
  1227. virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
  1228. PROT_READ | PROT_WRITE,
  1229. MAP_SHARED, 0);
  1230. }
  1231. if (virtual > -1024UL) {
  1232. /* Real error */
  1233. retcode = (signed long)virtual;
  1234. goto done;
  1235. }
  1236. request->virtual = (void __user *)virtual;
  1237. for (i = 0; i < dma->buf_count; i++) {
  1238. if (copy_to_user(&request->list[i].idx,
  1239. &dma->buflist[i]->idx,
  1240. sizeof(request->list[0].idx))) {
  1241. retcode = -EFAULT;
  1242. goto done;
  1243. }
  1244. if (copy_to_user(&request->list[i].total,
  1245. &dma->buflist[i]->total,
  1246. sizeof(request->list[0].total))) {
  1247. retcode = -EFAULT;
  1248. goto done;
  1249. }
  1250. if (copy_to_user(&request->list[i].used,
  1251. &zero, sizeof(zero))) {
  1252. retcode = -EFAULT;
  1253. goto done;
  1254. }
  1255. address = virtual + dma->buflist[i]->offset; /* *** */
  1256. if (copy_to_user(&request->list[i].address,
  1257. &address, sizeof(address))) {
  1258. retcode = -EFAULT;
  1259. goto done;
  1260. }
  1261. }
  1262. }
  1263. done:
  1264. request->count = dma->buf_count;
  1265. DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
  1266. return retcode;
  1267. }
  1268. int drm_dma_ioctl(struct drm_device *dev, void *data,
  1269. struct drm_file *file_priv)
  1270. {
  1271. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1272. return -EINVAL;
  1273. if (dev->driver->dma_ioctl)
  1274. return dev->driver->dma_ioctl(dev, data, file_priv);
  1275. else
  1276. return -EINVAL;
  1277. }
  1278. struct drm_local_map *drm_getsarea(struct drm_device *dev)
  1279. {
  1280. struct drm_map_list *entry;
  1281. list_for_each_entry(entry, &dev->maplist, head) {
  1282. if (entry->map && entry->map->type == _DRM_SHM &&
  1283. (entry->map->flags & _DRM_CONTAINS_LOCK)) {
  1284. return entry->map;
  1285. }
  1286. }
  1287. return NULL;
  1288. }
  1289. EXPORT_SYMBOL(drm_getsarea);