drm_bufs.c 45 KB

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