drm_bufs.c 45 KB

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