drm_bufs.c 42 KB

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