i915_gem.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552
  1. /*
  2. * Copyright © 2008 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include "drmP.h"
  28. #include "drm.h"
  29. #include "i915_drm.h"
  30. #include "i915_drv.h"
  31. #include <linux/swap.h>
  32. static int
  33. i915_gem_object_set_domain(struct drm_gem_object *obj,
  34. uint32_t read_domains,
  35. uint32_t write_domain);
  36. static int
  37. i915_gem_object_set_domain_range(struct drm_gem_object *obj,
  38. uint64_t offset,
  39. uint64_t size,
  40. uint32_t read_domains,
  41. uint32_t write_domain);
  42. static int
  43. i915_gem_set_domain(struct drm_gem_object *obj,
  44. struct drm_file *file_priv,
  45. uint32_t read_domains,
  46. uint32_t write_domain);
  47. static int i915_gem_object_get_page_list(struct drm_gem_object *obj);
  48. static void i915_gem_object_free_page_list(struct drm_gem_object *obj);
  49. static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
  50. int
  51. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  52. struct drm_file *file_priv)
  53. {
  54. drm_i915_private_t *dev_priv = dev->dev_private;
  55. struct drm_i915_gem_init *args = data;
  56. mutex_lock(&dev->struct_mutex);
  57. if (args->gtt_start >= args->gtt_end ||
  58. (args->gtt_start & (PAGE_SIZE - 1)) != 0 ||
  59. (args->gtt_end & (PAGE_SIZE - 1)) != 0) {
  60. mutex_unlock(&dev->struct_mutex);
  61. return -EINVAL;
  62. }
  63. drm_mm_init(&dev_priv->mm.gtt_space, args->gtt_start,
  64. args->gtt_end - args->gtt_start);
  65. dev->gtt_total = (uint32_t) (args->gtt_end - args->gtt_start);
  66. mutex_unlock(&dev->struct_mutex);
  67. return 0;
  68. }
  69. /**
  70. * Creates a new mm object and returns a handle to it.
  71. */
  72. int
  73. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  74. struct drm_file *file_priv)
  75. {
  76. struct drm_i915_gem_create *args = data;
  77. struct drm_gem_object *obj;
  78. int handle, ret;
  79. args->size = roundup(args->size, PAGE_SIZE);
  80. /* Allocate the new object */
  81. obj = drm_gem_object_alloc(dev, args->size);
  82. if (obj == NULL)
  83. return -ENOMEM;
  84. ret = drm_gem_handle_create(file_priv, obj, &handle);
  85. mutex_lock(&dev->struct_mutex);
  86. drm_gem_object_handle_unreference(obj);
  87. mutex_unlock(&dev->struct_mutex);
  88. if (ret)
  89. return ret;
  90. args->handle = handle;
  91. return 0;
  92. }
  93. /**
  94. * Reads data from the object referenced by handle.
  95. *
  96. * On error, the contents of *data are undefined.
  97. */
  98. int
  99. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  100. struct drm_file *file_priv)
  101. {
  102. struct drm_i915_gem_pread *args = data;
  103. struct drm_gem_object *obj;
  104. struct drm_i915_gem_object *obj_priv;
  105. ssize_t read;
  106. loff_t offset;
  107. int ret;
  108. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  109. if (obj == NULL)
  110. return -EBADF;
  111. obj_priv = obj->driver_private;
  112. /* Bounds check source.
  113. *
  114. * XXX: This could use review for overflow issues...
  115. */
  116. if (args->offset > obj->size || args->size > obj->size ||
  117. args->offset + args->size > obj->size) {
  118. drm_gem_object_unreference(obj);
  119. return -EINVAL;
  120. }
  121. mutex_lock(&dev->struct_mutex);
  122. ret = i915_gem_object_set_domain_range(obj, args->offset, args->size,
  123. I915_GEM_DOMAIN_CPU, 0);
  124. if (ret != 0) {
  125. drm_gem_object_unreference(obj);
  126. mutex_unlock(&dev->struct_mutex);
  127. return ret;
  128. }
  129. offset = args->offset;
  130. read = vfs_read(obj->filp, (char __user *)(uintptr_t)args->data_ptr,
  131. args->size, &offset);
  132. if (read != args->size) {
  133. drm_gem_object_unreference(obj);
  134. mutex_unlock(&dev->struct_mutex);
  135. if (read < 0)
  136. return read;
  137. else
  138. return -EINVAL;
  139. }
  140. drm_gem_object_unreference(obj);
  141. mutex_unlock(&dev->struct_mutex);
  142. return 0;
  143. }
  144. static int
  145. i915_gem_gtt_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  146. struct drm_i915_gem_pwrite *args,
  147. struct drm_file *file_priv)
  148. {
  149. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  150. ssize_t remain;
  151. loff_t offset;
  152. char __user *user_data;
  153. char __iomem *vaddr;
  154. char *vaddr_atomic;
  155. int i, o, l;
  156. int ret = 0;
  157. unsigned long pfn;
  158. unsigned long unwritten;
  159. user_data = (char __user *) (uintptr_t) args->data_ptr;
  160. remain = args->size;
  161. if (!access_ok(VERIFY_READ, user_data, remain))
  162. return -EFAULT;
  163. mutex_lock(&dev->struct_mutex);
  164. ret = i915_gem_object_pin(obj, 0);
  165. if (ret) {
  166. mutex_unlock(&dev->struct_mutex);
  167. return ret;
  168. }
  169. ret = i915_gem_set_domain(obj, file_priv,
  170. I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
  171. if (ret)
  172. goto fail;
  173. obj_priv = obj->driver_private;
  174. offset = obj_priv->gtt_offset + args->offset;
  175. obj_priv->dirty = 1;
  176. while (remain > 0) {
  177. /* Operation in this page
  178. *
  179. * i = page number
  180. * o = offset within page
  181. * l = bytes to copy
  182. */
  183. i = offset >> PAGE_SHIFT;
  184. o = offset & (PAGE_SIZE-1);
  185. l = remain;
  186. if ((o + l) > PAGE_SIZE)
  187. l = PAGE_SIZE - o;
  188. pfn = (dev->agp->base >> PAGE_SHIFT) + i;
  189. #ifdef CONFIG_HIGHMEM
  190. /* This is a workaround for the low performance of iounmap
  191. * (approximate 10% cpu cost on normal 3D workloads).
  192. * kmap_atomic on HIGHMEM kernels happens to let us map card
  193. * memory without taking IPIs. When the vmap rework lands
  194. * we should be able to dump this hack.
  195. */
  196. vaddr_atomic = kmap_atomic_pfn(pfn, KM_USER0);
  197. #if WATCH_PWRITE
  198. DRM_INFO("pwrite i %d o %d l %d pfn %ld vaddr %p\n",
  199. i, o, l, pfn, vaddr_atomic);
  200. #endif
  201. unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + o,
  202. user_data, l);
  203. kunmap_atomic(vaddr_atomic, KM_USER0);
  204. if (unwritten)
  205. #endif /* CONFIG_HIGHMEM */
  206. {
  207. vaddr = ioremap_wc(pfn << PAGE_SHIFT, PAGE_SIZE);
  208. #if WATCH_PWRITE
  209. DRM_INFO("pwrite slow i %d o %d l %d "
  210. "pfn %ld vaddr %p\n",
  211. i, o, l, pfn, vaddr);
  212. #endif
  213. if (vaddr == NULL) {
  214. ret = -EFAULT;
  215. goto fail;
  216. }
  217. unwritten = __copy_from_user(vaddr + o, user_data, l);
  218. #if WATCH_PWRITE
  219. DRM_INFO("unwritten %ld\n", unwritten);
  220. #endif
  221. iounmap(vaddr);
  222. if (unwritten) {
  223. ret = -EFAULT;
  224. goto fail;
  225. }
  226. }
  227. remain -= l;
  228. user_data += l;
  229. offset += l;
  230. }
  231. #if WATCH_PWRITE && 1
  232. i915_gem_clflush_object(obj);
  233. i915_gem_dump_object(obj, args->offset + args->size, __func__, ~0);
  234. i915_gem_clflush_object(obj);
  235. #endif
  236. fail:
  237. i915_gem_object_unpin(obj);
  238. mutex_unlock(&dev->struct_mutex);
  239. return ret;
  240. }
  241. static int
  242. i915_gem_shmem_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  243. struct drm_i915_gem_pwrite *args,
  244. struct drm_file *file_priv)
  245. {
  246. int ret;
  247. loff_t offset;
  248. ssize_t written;
  249. mutex_lock(&dev->struct_mutex);
  250. ret = i915_gem_set_domain(obj, file_priv,
  251. I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
  252. if (ret) {
  253. mutex_unlock(&dev->struct_mutex);
  254. return ret;
  255. }
  256. offset = args->offset;
  257. written = vfs_write(obj->filp,
  258. (char __user *)(uintptr_t) args->data_ptr,
  259. args->size, &offset);
  260. if (written != args->size) {
  261. mutex_unlock(&dev->struct_mutex);
  262. if (written < 0)
  263. return written;
  264. else
  265. return -EINVAL;
  266. }
  267. mutex_unlock(&dev->struct_mutex);
  268. return 0;
  269. }
  270. /**
  271. * Writes data to the object referenced by handle.
  272. *
  273. * On error, the contents of the buffer that were to be modified are undefined.
  274. */
  275. int
  276. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  277. struct drm_file *file_priv)
  278. {
  279. struct drm_i915_gem_pwrite *args = data;
  280. struct drm_gem_object *obj;
  281. struct drm_i915_gem_object *obj_priv;
  282. int ret = 0;
  283. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  284. if (obj == NULL)
  285. return -EBADF;
  286. obj_priv = obj->driver_private;
  287. /* Bounds check destination.
  288. *
  289. * XXX: This could use review for overflow issues...
  290. */
  291. if (args->offset > obj->size || args->size > obj->size ||
  292. args->offset + args->size > obj->size) {
  293. drm_gem_object_unreference(obj);
  294. return -EINVAL;
  295. }
  296. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  297. * it would end up going through the fenced access, and we'll get
  298. * different detiling behavior between reading and writing.
  299. * pread/pwrite currently are reading and writing from the CPU
  300. * perspective, requiring manual detiling by the client.
  301. */
  302. if (obj_priv->tiling_mode == I915_TILING_NONE &&
  303. dev->gtt_total != 0)
  304. ret = i915_gem_gtt_pwrite(dev, obj, args, file_priv);
  305. else
  306. ret = i915_gem_shmem_pwrite(dev, obj, args, file_priv);
  307. #if WATCH_PWRITE
  308. if (ret)
  309. DRM_INFO("pwrite failed %d\n", ret);
  310. #endif
  311. drm_gem_object_unreference(obj);
  312. return ret;
  313. }
  314. /**
  315. * Called when user space prepares to use an object
  316. */
  317. int
  318. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  319. struct drm_file *file_priv)
  320. {
  321. struct drm_i915_gem_set_domain *args = data;
  322. struct drm_gem_object *obj;
  323. int ret;
  324. if (!(dev->driver->driver_features & DRIVER_GEM))
  325. return -ENODEV;
  326. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  327. if (obj == NULL)
  328. return -EBADF;
  329. mutex_lock(&dev->struct_mutex);
  330. #if WATCH_BUF
  331. DRM_INFO("set_domain_ioctl %p(%d), %08x %08x\n",
  332. obj, obj->size, args->read_domains, args->write_domain);
  333. #endif
  334. ret = i915_gem_set_domain(obj, file_priv,
  335. args->read_domains, args->write_domain);
  336. drm_gem_object_unreference(obj);
  337. mutex_unlock(&dev->struct_mutex);
  338. return ret;
  339. }
  340. /**
  341. * Called when user space has done writes to this buffer
  342. */
  343. int
  344. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  345. struct drm_file *file_priv)
  346. {
  347. struct drm_i915_gem_sw_finish *args = data;
  348. struct drm_gem_object *obj;
  349. struct drm_i915_gem_object *obj_priv;
  350. int ret = 0;
  351. if (!(dev->driver->driver_features & DRIVER_GEM))
  352. return -ENODEV;
  353. mutex_lock(&dev->struct_mutex);
  354. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  355. if (obj == NULL) {
  356. mutex_unlock(&dev->struct_mutex);
  357. return -EBADF;
  358. }
  359. #if WATCH_BUF
  360. DRM_INFO("%s: sw_finish %d (%p %d)\n",
  361. __func__, args->handle, obj, obj->size);
  362. #endif
  363. obj_priv = obj->driver_private;
  364. /* Pinned buffers may be scanout, so flush the cache */
  365. if ((obj->write_domain & I915_GEM_DOMAIN_CPU) && obj_priv->pin_count) {
  366. i915_gem_clflush_object(obj);
  367. drm_agp_chipset_flush(dev);
  368. }
  369. drm_gem_object_unreference(obj);
  370. mutex_unlock(&dev->struct_mutex);
  371. return ret;
  372. }
  373. /**
  374. * Maps the contents of an object, returning the address it is mapped
  375. * into.
  376. *
  377. * While the mapping holds a reference on the contents of the object, it doesn't
  378. * imply a ref on the object itself.
  379. */
  380. int
  381. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  382. struct drm_file *file_priv)
  383. {
  384. struct drm_i915_gem_mmap *args = data;
  385. struct drm_gem_object *obj;
  386. loff_t offset;
  387. unsigned long addr;
  388. if (!(dev->driver->driver_features & DRIVER_GEM))
  389. return -ENODEV;
  390. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  391. if (obj == NULL)
  392. return -EBADF;
  393. offset = args->offset;
  394. down_write(&current->mm->mmap_sem);
  395. addr = do_mmap(obj->filp, 0, args->size,
  396. PROT_READ | PROT_WRITE, MAP_SHARED,
  397. args->offset);
  398. up_write(&current->mm->mmap_sem);
  399. mutex_lock(&dev->struct_mutex);
  400. drm_gem_object_unreference(obj);
  401. mutex_unlock(&dev->struct_mutex);
  402. if (IS_ERR((void *)addr))
  403. return addr;
  404. args->addr_ptr = (uint64_t) addr;
  405. return 0;
  406. }
  407. static void
  408. i915_gem_object_free_page_list(struct drm_gem_object *obj)
  409. {
  410. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  411. int page_count = obj->size / PAGE_SIZE;
  412. int i;
  413. if (obj_priv->page_list == NULL)
  414. return;
  415. for (i = 0; i < page_count; i++)
  416. if (obj_priv->page_list[i] != NULL) {
  417. if (obj_priv->dirty)
  418. set_page_dirty(obj_priv->page_list[i]);
  419. mark_page_accessed(obj_priv->page_list[i]);
  420. page_cache_release(obj_priv->page_list[i]);
  421. }
  422. obj_priv->dirty = 0;
  423. drm_free(obj_priv->page_list,
  424. page_count * sizeof(struct page *),
  425. DRM_MEM_DRIVER);
  426. obj_priv->page_list = NULL;
  427. }
  428. static void
  429. i915_gem_object_move_to_active(struct drm_gem_object *obj)
  430. {
  431. struct drm_device *dev = obj->dev;
  432. drm_i915_private_t *dev_priv = dev->dev_private;
  433. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  434. /* Add a reference if we're newly entering the active list. */
  435. if (!obj_priv->active) {
  436. drm_gem_object_reference(obj);
  437. obj_priv->active = 1;
  438. }
  439. /* Move from whatever list we were on to the tail of execution. */
  440. list_move_tail(&obj_priv->list,
  441. &dev_priv->mm.active_list);
  442. }
  443. static void
  444. i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
  445. {
  446. struct drm_device *dev = obj->dev;
  447. drm_i915_private_t *dev_priv = dev->dev_private;
  448. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  449. i915_verify_inactive(dev, __FILE__, __LINE__);
  450. if (obj_priv->pin_count != 0)
  451. list_del_init(&obj_priv->list);
  452. else
  453. list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
  454. if (obj_priv->active) {
  455. obj_priv->active = 0;
  456. drm_gem_object_unreference(obj);
  457. }
  458. i915_verify_inactive(dev, __FILE__, __LINE__);
  459. }
  460. /**
  461. * Creates a new sequence number, emitting a write of it to the status page
  462. * plus an interrupt, which will trigger i915_user_interrupt_handler.
  463. *
  464. * Must be called with struct_lock held.
  465. *
  466. * Returned sequence numbers are nonzero on success.
  467. */
  468. static uint32_t
  469. i915_add_request(struct drm_device *dev, uint32_t flush_domains)
  470. {
  471. drm_i915_private_t *dev_priv = dev->dev_private;
  472. struct drm_i915_gem_request *request;
  473. uint32_t seqno;
  474. int was_empty;
  475. RING_LOCALS;
  476. request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER);
  477. if (request == NULL)
  478. return 0;
  479. /* Grab the seqno we're going to make this request be, and bump the
  480. * next (skipping 0 so it can be the reserved no-seqno value).
  481. */
  482. seqno = dev_priv->mm.next_gem_seqno;
  483. dev_priv->mm.next_gem_seqno++;
  484. if (dev_priv->mm.next_gem_seqno == 0)
  485. dev_priv->mm.next_gem_seqno++;
  486. BEGIN_LP_RING(4);
  487. OUT_RING(MI_STORE_DWORD_INDEX);
  488. OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
  489. OUT_RING(seqno);
  490. OUT_RING(MI_USER_INTERRUPT);
  491. ADVANCE_LP_RING();
  492. DRM_DEBUG("%d\n", seqno);
  493. request->seqno = seqno;
  494. request->emitted_jiffies = jiffies;
  495. request->flush_domains = flush_domains;
  496. was_empty = list_empty(&dev_priv->mm.request_list);
  497. list_add_tail(&request->list, &dev_priv->mm.request_list);
  498. if (was_empty)
  499. schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
  500. return seqno;
  501. }
  502. /**
  503. * Command execution barrier
  504. *
  505. * Ensures that all commands in the ring are finished
  506. * before signalling the CPU
  507. */
  508. static uint32_t
  509. i915_retire_commands(struct drm_device *dev)
  510. {
  511. drm_i915_private_t *dev_priv = dev->dev_private;
  512. uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
  513. uint32_t flush_domains = 0;
  514. RING_LOCALS;
  515. /* The sampler always gets flushed on i965 (sigh) */
  516. if (IS_I965G(dev))
  517. flush_domains |= I915_GEM_DOMAIN_SAMPLER;
  518. BEGIN_LP_RING(2);
  519. OUT_RING(cmd);
  520. OUT_RING(0); /* noop */
  521. ADVANCE_LP_RING();
  522. return flush_domains;
  523. }
  524. /**
  525. * Moves buffers associated only with the given active seqno from the active
  526. * to inactive list, potentially freeing them.
  527. */
  528. static void
  529. i915_gem_retire_request(struct drm_device *dev,
  530. struct drm_i915_gem_request *request)
  531. {
  532. drm_i915_private_t *dev_priv = dev->dev_private;
  533. /* Move any buffers on the active list that are no longer referenced
  534. * by the ringbuffer to the flushing/inactive lists as appropriate.
  535. */
  536. while (!list_empty(&dev_priv->mm.active_list)) {
  537. struct drm_gem_object *obj;
  538. struct drm_i915_gem_object *obj_priv;
  539. obj_priv = list_first_entry(&dev_priv->mm.active_list,
  540. struct drm_i915_gem_object,
  541. list);
  542. obj = obj_priv->obj;
  543. /* If the seqno being retired doesn't match the oldest in the
  544. * list, then the oldest in the list must still be newer than
  545. * this seqno.
  546. */
  547. if (obj_priv->last_rendering_seqno != request->seqno)
  548. return;
  549. #if WATCH_LRU
  550. DRM_INFO("%s: retire %d moves to inactive list %p\n",
  551. __func__, request->seqno, obj);
  552. #endif
  553. if (obj->write_domain != 0) {
  554. list_move_tail(&obj_priv->list,
  555. &dev_priv->mm.flushing_list);
  556. } else {
  557. i915_gem_object_move_to_inactive(obj);
  558. }
  559. }
  560. if (request->flush_domains != 0) {
  561. struct drm_i915_gem_object *obj_priv, *next;
  562. /* Clear the write domain and activity from any buffers
  563. * that are just waiting for a flush matching the one retired.
  564. */
  565. list_for_each_entry_safe(obj_priv, next,
  566. &dev_priv->mm.flushing_list, list) {
  567. struct drm_gem_object *obj = obj_priv->obj;
  568. if (obj->write_domain & request->flush_domains) {
  569. obj->write_domain = 0;
  570. i915_gem_object_move_to_inactive(obj);
  571. }
  572. }
  573. }
  574. }
  575. /**
  576. * Returns true if seq1 is later than seq2.
  577. */
  578. static int
  579. i915_seqno_passed(uint32_t seq1, uint32_t seq2)
  580. {
  581. return (int32_t)(seq1 - seq2) >= 0;
  582. }
  583. uint32_t
  584. i915_get_gem_seqno(struct drm_device *dev)
  585. {
  586. drm_i915_private_t *dev_priv = dev->dev_private;
  587. return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
  588. }
  589. /**
  590. * This function clears the request list as sequence numbers are passed.
  591. */
  592. void
  593. i915_gem_retire_requests(struct drm_device *dev)
  594. {
  595. drm_i915_private_t *dev_priv = dev->dev_private;
  596. uint32_t seqno;
  597. seqno = i915_get_gem_seqno(dev);
  598. while (!list_empty(&dev_priv->mm.request_list)) {
  599. struct drm_i915_gem_request *request;
  600. uint32_t retiring_seqno;
  601. request = list_first_entry(&dev_priv->mm.request_list,
  602. struct drm_i915_gem_request,
  603. list);
  604. retiring_seqno = request->seqno;
  605. if (i915_seqno_passed(seqno, retiring_seqno) ||
  606. dev_priv->mm.wedged) {
  607. i915_gem_retire_request(dev, request);
  608. list_del(&request->list);
  609. drm_free(request, sizeof(*request), DRM_MEM_DRIVER);
  610. } else
  611. break;
  612. }
  613. }
  614. void
  615. i915_gem_retire_work_handler(struct work_struct *work)
  616. {
  617. drm_i915_private_t *dev_priv;
  618. struct drm_device *dev;
  619. dev_priv = container_of(work, drm_i915_private_t,
  620. mm.retire_work.work);
  621. dev = dev_priv->dev;
  622. mutex_lock(&dev->struct_mutex);
  623. i915_gem_retire_requests(dev);
  624. if (!list_empty(&dev_priv->mm.request_list))
  625. schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
  626. mutex_unlock(&dev->struct_mutex);
  627. }
  628. /**
  629. * Waits for a sequence number to be signaled, and cleans up the
  630. * request and object lists appropriately for that event.
  631. */
  632. static int
  633. i915_wait_request(struct drm_device *dev, uint32_t seqno)
  634. {
  635. drm_i915_private_t *dev_priv = dev->dev_private;
  636. int ret = 0;
  637. BUG_ON(seqno == 0);
  638. if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
  639. dev_priv->mm.waiting_gem_seqno = seqno;
  640. i915_user_irq_get(dev);
  641. ret = wait_event_interruptible(dev_priv->irq_queue,
  642. i915_seqno_passed(i915_get_gem_seqno(dev),
  643. seqno) ||
  644. dev_priv->mm.wedged);
  645. i915_user_irq_put(dev);
  646. dev_priv->mm.waiting_gem_seqno = 0;
  647. }
  648. if (dev_priv->mm.wedged)
  649. ret = -EIO;
  650. if (ret && ret != -ERESTARTSYS)
  651. DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
  652. __func__, ret, seqno, i915_get_gem_seqno(dev));
  653. /* Directly dispatch request retiring. While we have the work queue
  654. * to handle this, the waiter on a request often wants an associated
  655. * buffer to have made it to the inactive list, and we would need
  656. * a separate wait queue to handle that.
  657. */
  658. if (ret == 0)
  659. i915_gem_retire_requests(dev);
  660. return ret;
  661. }
  662. static void
  663. i915_gem_flush(struct drm_device *dev,
  664. uint32_t invalidate_domains,
  665. uint32_t flush_domains)
  666. {
  667. drm_i915_private_t *dev_priv = dev->dev_private;
  668. uint32_t cmd;
  669. RING_LOCALS;
  670. #if WATCH_EXEC
  671. DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
  672. invalidate_domains, flush_domains);
  673. #endif
  674. if (flush_domains & I915_GEM_DOMAIN_CPU)
  675. drm_agp_chipset_flush(dev);
  676. if ((invalidate_domains | flush_domains) & ~(I915_GEM_DOMAIN_CPU |
  677. I915_GEM_DOMAIN_GTT)) {
  678. /*
  679. * read/write caches:
  680. *
  681. * I915_GEM_DOMAIN_RENDER is always invalidated, but is
  682. * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
  683. * also flushed at 2d versus 3d pipeline switches.
  684. *
  685. * read-only caches:
  686. *
  687. * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
  688. * MI_READ_FLUSH is set, and is always flushed on 965.
  689. *
  690. * I915_GEM_DOMAIN_COMMAND may not exist?
  691. *
  692. * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
  693. * invalidated when MI_EXE_FLUSH is set.
  694. *
  695. * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
  696. * invalidated with every MI_FLUSH.
  697. *
  698. * TLBs:
  699. *
  700. * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
  701. * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
  702. * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
  703. * are flushed at any MI_FLUSH.
  704. */
  705. cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
  706. if ((invalidate_domains|flush_domains) &
  707. I915_GEM_DOMAIN_RENDER)
  708. cmd &= ~MI_NO_WRITE_FLUSH;
  709. if (!IS_I965G(dev)) {
  710. /*
  711. * On the 965, the sampler cache always gets flushed
  712. * and this bit is reserved.
  713. */
  714. if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
  715. cmd |= MI_READ_FLUSH;
  716. }
  717. if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
  718. cmd |= MI_EXE_FLUSH;
  719. #if WATCH_EXEC
  720. DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
  721. #endif
  722. BEGIN_LP_RING(2);
  723. OUT_RING(cmd);
  724. OUT_RING(0); /* noop */
  725. ADVANCE_LP_RING();
  726. }
  727. }
  728. /**
  729. * Ensures that all rendering to the object has completed and the object is
  730. * safe to unbind from the GTT or access from the CPU.
  731. */
  732. static int
  733. i915_gem_object_wait_rendering(struct drm_gem_object *obj)
  734. {
  735. struct drm_device *dev = obj->dev;
  736. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  737. int ret;
  738. /* If there are writes queued to the buffer, flush and
  739. * create a new seqno to wait for.
  740. */
  741. if (obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)) {
  742. uint32_t write_domain = obj->write_domain;
  743. #if WATCH_BUF
  744. DRM_INFO("%s: flushing object %p from write domain %08x\n",
  745. __func__, obj, write_domain);
  746. #endif
  747. i915_gem_flush(dev, 0, write_domain);
  748. i915_gem_object_move_to_active(obj);
  749. obj_priv->last_rendering_seqno = i915_add_request(dev,
  750. write_domain);
  751. BUG_ON(obj_priv->last_rendering_seqno == 0);
  752. #if WATCH_LRU
  753. DRM_INFO("%s: flush moves to exec list %p\n", __func__, obj);
  754. #endif
  755. }
  756. /* If there is rendering queued on the buffer being evicted, wait for
  757. * it.
  758. */
  759. if (obj_priv->active) {
  760. #if WATCH_BUF
  761. DRM_INFO("%s: object %p wait for seqno %08x\n",
  762. __func__, obj, obj_priv->last_rendering_seqno);
  763. #endif
  764. ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
  765. if (ret != 0)
  766. return ret;
  767. }
  768. return 0;
  769. }
  770. /**
  771. * Unbinds an object from the GTT aperture.
  772. */
  773. static int
  774. i915_gem_object_unbind(struct drm_gem_object *obj)
  775. {
  776. struct drm_device *dev = obj->dev;
  777. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  778. int ret = 0;
  779. #if WATCH_BUF
  780. DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
  781. DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
  782. #endif
  783. if (obj_priv->gtt_space == NULL)
  784. return 0;
  785. if (obj_priv->pin_count != 0) {
  786. DRM_ERROR("Attempting to unbind pinned buffer\n");
  787. return -EINVAL;
  788. }
  789. /* Wait for any rendering to complete
  790. */
  791. ret = i915_gem_object_wait_rendering(obj);
  792. if (ret) {
  793. DRM_ERROR("wait_rendering failed: %d\n", ret);
  794. return ret;
  795. }
  796. /* Move the object to the CPU domain to ensure that
  797. * any possible CPU writes while it's not in the GTT
  798. * are flushed when we go to remap it. This will
  799. * also ensure that all pending GPU writes are finished
  800. * before we unbind.
  801. */
  802. ret = i915_gem_object_set_domain(obj, I915_GEM_DOMAIN_CPU,
  803. I915_GEM_DOMAIN_CPU);
  804. if (ret) {
  805. DRM_ERROR("set_domain failed: %d\n", ret);
  806. return ret;
  807. }
  808. if (obj_priv->agp_mem != NULL) {
  809. drm_unbind_agp(obj_priv->agp_mem);
  810. drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
  811. obj_priv->agp_mem = NULL;
  812. }
  813. BUG_ON(obj_priv->active);
  814. i915_gem_object_free_page_list(obj);
  815. if (obj_priv->gtt_space) {
  816. atomic_dec(&dev->gtt_count);
  817. atomic_sub(obj->size, &dev->gtt_memory);
  818. drm_mm_put_block(obj_priv->gtt_space);
  819. obj_priv->gtt_space = NULL;
  820. }
  821. /* Remove ourselves from the LRU list if present. */
  822. if (!list_empty(&obj_priv->list))
  823. list_del_init(&obj_priv->list);
  824. return 0;
  825. }
  826. static int
  827. i915_gem_evict_something(struct drm_device *dev)
  828. {
  829. drm_i915_private_t *dev_priv = dev->dev_private;
  830. struct drm_gem_object *obj;
  831. struct drm_i915_gem_object *obj_priv;
  832. int ret = 0;
  833. for (;;) {
  834. /* If there's an inactive buffer available now, grab it
  835. * and be done.
  836. */
  837. if (!list_empty(&dev_priv->mm.inactive_list)) {
  838. obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
  839. struct drm_i915_gem_object,
  840. list);
  841. obj = obj_priv->obj;
  842. BUG_ON(obj_priv->pin_count != 0);
  843. #if WATCH_LRU
  844. DRM_INFO("%s: evicting %p\n", __func__, obj);
  845. #endif
  846. BUG_ON(obj_priv->active);
  847. /* Wait on the rendering and unbind the buffer. */
  848. ret = i915_gem_object_unbind(obj);
  849. break;
  850. }
  851. /* If we didn't get anything, but the ring is still processing
  852. * things, wait for one of those things to finish and hopefully
  853. * leave us a buffer to evict.
  854. */
  855. if (!list_empty(&dev_priv->mm.request_list)) {
  856. struct drm_i915_gem_request *request;
  857. request = list_first_entry(&dev_priv->mm.request_list,
  858. struct drm_i915_gem_request,
  859. list);
  860. ret = i915_wait_request(dev, request->seqno);
  861. if (ret)
  862. break;
  863. /* if waiting caused an object to become inactive,
  864. * then loop around and wait for it. Otherwise, we
  865. * assume that waiting freed and unbound something,
  866. * so there should now be some space in the GTT
  867. */
  868. if (!list_empty(&dev_priv->mm.inactive_list))
  869. continue;
  870. break;
  871. }
  872. /* If we didn't have anything on the request list but there
  873. * are buffers awaiting a flush, emit one and try again.
  874. * When we wait on it, those buffers waiting for that flush
  875. * will get moved to inactive.
  876. */
  877. if (!list_empty(&dev_priv->mm.flushing_list)) {
  878. obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
  879. struct drm_i915_gem_object,
  880. list);
  881. obj = obj_priv->obj;
  882. i915_gem_flush(dev,
  883. obj->write_domain,
  884. obj->write_domain);
  885. i915_add_request(dev, obj->write_domain);
  886. obj = NULL;
  887. continue;
  888. }
  889. DRM_ERROR("inactive empty %d request empty %d "
  890. "flushing empty %d\n",
  891. list_empty(&dev_priv->mm.inactive_list),
  892. list_empty(&dev_priv->mm.request_list),
  893. list_empty(&dev_priv->mm.flushing_list));
  894. /* If we didn't do any of the above, there's nothing to be done
  895. * and we just can't fit it in.
  896. */
  897. return -ENOMEM;
  898. }
  899. return ret;
  900. }
  901. static int
  902. i915_gem_object_get_page_list(struct drm_gem_object *obj)
  903. {
  904. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  905. int page_count, i;
  906. struct address_space *mapping;
  907. struct inode *inode;
  908. struct page *page;
  909. int ret;
  910. if (obj_priv->page_list)
  911. return 0;
  912. /* Get the list of pages out of our struct file. They'll be pinned
  913. * at this point until we release them.
  914. */
  915. page_count = obj->size / PAGE_SIZE;
  916. BUG_ON(obj_priv->page_list != NULL);
  917. obj_priv->page_list = drm_calloc(page_count, sizeof(struct page *),
  918. DRM_MEM_DRIVER);
  919. if (obj_priv->page_list == NULL) {
  920. DRM_ERROR("Faled to allocate page list\n");
  921. return -ENOMEM;
  922. }
  923. inode = obj->filp->f_path.dentry->d_inode;
  924. mapping = inode->i_mapping;
  925. for (i = 0; i < page_count; i++) {
  926. page = read_mapping_page(mapping, i, NULL);
  927. if (IS_ERR(page)) {
  928. ret = PTR_ERR(page);
  929. DRM_ERROR("read_mapping_page failed: %d\n", ret);
  930. i915_gem_object_free_page_list(obj);
  931. return ret;
  932. }
  933. obj_priv->page_list[i] = page;
  934. }
  935. return 0;
  936. }
  937. /**
  938. * Finds free space in the GTT aperture and binds the object there.
  939. */
  940. static int
  941. i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
  942. {
  943. struct drm_device *dev = obj->dev;
  944. drm_i915_private_t *dev_priv = dev->dev_private;
  945. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  946. struct drm_mm_node *free_space;
  947. int page_count, ret;
  948. if (alignment == 0)
  949. alignment = PAGE_SIZE;
  950. if (alignment & (PAGE_SIZE - 1)) {
  951. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  952. return -EINVAL;
  953. }
  954. search_free:
  955. free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
  956. obj->size, alignment, 0);
  957. if (free_space != NULL) {
  958. obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
  959. alignment);
  960. if (obj_priv->gtt_space != NULL) {
  961. obj_priv->gtt_space->private = obj;
  962. obj_priv->gtt_offset = obj_priv->gtt_space->start;
  963. }
  964. }
  965. if (obj_priv->gtt_space == NULL) {
  966. /* If the gtt is empty and we're still having trouble
  967. * fitting our object in, we're out of memory.
  968. */
  969. #if WATCH_LRU
  970. DRM_INFO("%s: GTT full, evicting something\n", __func__);
  971. #endif
  972. if (list_empty(&dev_priv->mm.inactive_list) &&
  973. list_empty(&dev_priv->mm.flushing_list) &&
  974. list_empty(&dev_priv->mm.active_list)) {
  975. DRM_ERROR("GTT full, but LRU list empty\n");
  976. return -ENOMEM;
  977. }
  978. ret = i915_gem_evict_something(dev);
  979. if (ret != 0) {
  980. DRM_ERROR("Failed to evict a buffer %d\n", ret);
  981. return ret;
  982. }
  983. goto search_free;
  984. }
  985. #if WATCH_BUF
  986. DRM_INFO("Binding object of size %d at 0x%08x\n",
  987. obj->size, obj_priv->gtt_offset);
  988. #endif
  989. ret = i915_gem_object_get_page_list(obj);
  990. if (ret) {
  991. drm_mm_put_block(obj_priv->gtt_space);
  992. obj_priv->gtt_space = NULL;
  993. return ret;
  994. }
  995. page_count = obj->size / PAGE_SIZE;
  996. /* Create an AGP memory structure pointing at our pages, and bind it
  997. * into the GTT.
  998. */
  999. obj_priv->agp_mem = drm_agp_bind_pages(dev,
  1000. obj_priv->page_list,
  1001. page_count,
  1002. obj_priv->gtt_offset,
  1003. obj_priv->agp_type);
  1004. if (obj_priv->agp_mem == NULL) {
  1005. i915_gem_object_free_page_list(obj);
  1006. drm_mm_put_block(obj_priv->gtt_space);
  1007. obj_priv->gtt_space = NULL;
  1008. return -ENOMEM;
  1009. }
  1010. atomic_inc(&dev->gtt_count);
  1011. atomic_add(obj->size, &dev->gtt_memory);
  1012. /* Assert that the object is not currently in any GPU domain. As it
  1013. * wasn't in the GTT, there shouldn't be any way it could have been in
  1014. * a GPU cache
  1015. */
  1016. BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  1017. BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  1018. return 0;
  1019. }
  1020. void
  1021. i915_gem_clflush_object(struct drm_gem_object *obj)
  1022. {
  1023. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1024. /* If we don't have a page list set up, then we're not pinned
  1025. * to GPU, and we can ignore the cache flush because it'll happen
  1026. * again at bind time.
  1027. */
  1028. if (obj_priv->page_list == NULL)
  1029. return;
  1030. drm_clflush_pages(obj_priv->page_list, obj->size / PAGE_SIZE);
  1031. }
  1032. /*
  1033. * Set the next domain for the specified object. This
  1034. * may not actually perform the necessary flushing/invaliding though,
  1035. * as that may want to be batched with other set_domain operations
  1036. *
  1037. * This is (we hope) the only really tricky part of gem. The goal
  1038. * is fairly simple -- track which caches hold bits of the object
  1039. * and make sure they remain coherent. A few concrete examples may
  1040. * help to explain how it works. For shorthand, we use the notation
  1041. * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
  1042. * a pair of read and write domain masks.
  1043. *
  1044. * Case 1: the batch buffer
  1045. *
  1046. * 1. Allocated
  1047. * 2. Written by CPU
  1048. * 3. Mapped to GTT
  1049. * 4. Read by GPU
  1050. * 5. Unmapped from GTT
  1051. * 6. Freed
  1052. *
  1053. * Let's take these a step at a time
  1054. *
  1055. * 1. Allocated
  1056. * Pages allocated from the kernel may still have
  1057. * cache contents, so we set them to (CPU, CPU) always.
  1058. * 2. Written by CPU (using pwrite)
  1059. * The pwrite function calls set_domain (CPU, CPU) and
  1060. * this function does nothing (as nothing changes)
  1061. * 3. Mapped by GTT
  1062. * This function asserts that the object is not
  1063. * currently in any GPU-based read or write domains
  1064. * 4. Read by GPU
  1065. * i915_gem_execbuffer calls set_domain (COMMAND, 0).
  1066. * As write_domain is zero, this function adds in the
  1067. * current read domains (CPU+COMMAND, 0).
  1068. * flush_domains is set to CPU.
  1069. * invalidate_domains is set to COMMAND
  1070. * clflush is run to get data out of the CPU caches
  1071. * then i915_dev_set_domain calls i915_gem_flush to
  1072. * emit an MI_FLUSH and drm_agp_chipset_flush
  1073. * 5. Unmapped from GTT
  1074. * i915_gem_object_unbind calls set_domain (CPU, CPU)
  1075. * flush_domains and invalidate_domains end up both zero
  1076. * so no flushing/invalidating happens
  1077. * 6. Freed
  1078. * yay, done
  1079. *
  1080. * Case 2: The shared render buffer
  1081. *
  1082. * 1. Allocated
  1083. * 2. Mapped to GTT
  1084. * 3. Read/written by GPU
  1085. * 4. set_domain to (CPU,CPU)
  1086. * 5. Read/written by CPU
  1087. * 6. Read/written by GPU
  1088. *
  1089. * 1. Allocated
  1090. * Same as last example, (CPU, CPU)
  1091. * 2. Mapped to GTT
  1092. * Nothing changes (assertions find that it is not in the GPU)
  1093. * 3. Read/written by GPU
  1094. * execbuffer calls set_domain (RENDER, RENDER)
  1095. * flush_domains gets CPU
  1096. * invalidate_domains gets GPU
  1097. * clflush (obj)
  1098. * MI_FLUSH and drm_agp_chipset_flush
  1099. * 4. set_domain (CPU, CPU)
  1100. * flush_domains gets GPU
  1101. * invalidate_domains gets CPU
  1102. * wait_rendering (obj) to make sure all drawing is complete.
  1103. * This will include an MI_FLUSH to get the data from GPU
  1104. * to memory
  1105. * clflush (obj) to invalidate the CPU cache
  1106. * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
  1107. * 5. Read/written by CPU
  1108. * cache lines are loaded and dirtied
  1109. * 6. Read written by GPU
  1110. * Same as last GPU access
  1111. *
  1112. * Case 3: The constant buffer
  1113. *
  1114. * 1. Allocated
  1115. * 2. Written by CPU
  1116. * 3. Read by GPU
  1117. * 4. Updated (written) by CPU again
  1118. * 5. Read by GPU
  1119. *
  1120. * 1. Allocated
  1121. * (CPU, CPU)
  1122. * 2. Written by CPU
  1123. * (CPU, CPU)
  1124. * 3. Read by GPU
  1125. * (CPU+RENDER, 0)
  1126. * flush_domains = CPU
  1127. * invalidate_domains = RENDER
  1128. * clflush (obj)
  1129. * MI_FLUSH
  1130. * drm_agp_chipset_flush
  1131. * 4. Updated (written) by CPU again
  1132. * (CPU, CPU)
  1133. * flush_domains = 0 (no previous write domain)
  1134. * invalidate_domains = 0 (no new read domains)
  1135. * 5. Read by GPU
  1136. * (CPU+RENDER, 0)
  1137. * flush_domains = CPU
  1138. * invalidate_domains = RENDER
  1139. * clflush (obj)
  1140. * MI_FLUSH
  1141. * drm_agp_chipset_flush
  1142. */
  1143. static int
  1144. i915_gem_object_set_domain(struct drm_gem_object *obj,
  1145. uint32_t read_domains,
  1146. uint32_t write_domain)
  1147. {
  1148. struct drm_device *dev = obj->dev;
  1149. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1150. uint32_t invalidate_domains = 0;
  1151. uint32_t flush_domains = 0;
  1152. int ret;
  1153. #if WATCH_BUF
  1154. DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
  1155. __func__, obj,
  1156. obj->read_domains, read_domains,
  1157. obj->write_domain, write_domain);
  1158. #endif
  1159. /*
  1160. * If the object isn't moving to a new write domain,
  1161. * let the object stay in multiple read domains
  1162. */
  1163. if (write_domain == 0)
  1164. read_domains |= obj->read_domains;
  1165. else
  1166. obj_priv->dirty = 1;
  1167. /*
  1168. * Flush the current write domain if
  1169. * the new read domains don't match. Invalidate
  1170. * any read domains which differ from the old
  1171. * write domain
  1172. */
  1173. if (obj->write_domain && obj->write_domain != read_domains) {
  1174. flush_domains |= obj->write_domain;
  1175. invalidate_domains |= read_domains & ~obj->write_domain;
  1176. }
  1177. /*
  1178. * Invalidate any read caches which may have
  1179. * stale data. That is, any new read domains.
  1180. */
  1181. invalidate_domains |= read_domains & ~obj->read_domains;
  1182. if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
  1183. #if WATCH_BUF
  1184. DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
  1185. __func__, flush_domains, invalidate_domains);
  1186. #endif
  1187. /*
  1188. * If we're invaliding the CPU cache and flushing a GPU cache,
  1189. * then pause for rendering so that the GPU caches will be
  1190. * flushed before the cpu cache is invalidated
  1191. */
  1192. if ((invalidate_domains & I915_GEM_DOMAIN_CPU) &&
  1193. (flush_domains & ~(I915_GEM_DOMAIN_CPU |
  1194. I915_GEM_DOMAIN_GTT))) {
  1195. ret = i915_gem_object_wait_rendering(obj);
  1196. if (ret)
  1197. return ret;
  1198. }
  1199. i915_gem_clflush_object(obj);
  1200. }
  1201. if ((write_domain | flush_domains) != 0)
  1202. obj->write_domain = write_domain;
  1203. /* If we're invalidating the CPU domain, clear the per-page CPU
  1204. * domain list as well.
  1205. */
  1206. if (obj_priv->page_cpu_valid != NULL &&
  1207. (write_domain != 0 ||
  1208. read_domains & I915_GEM_DOMAIN_CPU)) {
  1209. drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
  1210. DRM_MEM_DRIVER);
  1211. obj_priv->page_cpu_valid = NULL;
  1212. }
  1213. obj->read_domains = read_domains;
  1214. dev->invalidate_domains |= invalidate_domains;
  1215. dev->flush_domains |= flush_domains;
  1216. #if WATCH_BUF
  1217. DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
  1218. __func__,
  1219. obj->read_domains, obj->write_domain,
  1220. dev->invalidate_domains, dev->flush_domains);
  1221. #endif
  1222. return 0;
  1223. }
  1224. /**
  1225. * Set the read/write domain on a range of the object.
  1226. *
  1227. * Currently only implemented for CPU reads, otherwise drops to normal
  1228. * i915_gem_object_set_domain().
  1229. */
  1230. static int
  1231. i915_gem_object_set_domain_range(struct drm_gem_object *obj,
  1232. uint64_t offset,
  1233. uint64_t size,
  1234. uint32_t read_domains,
  1235. uint32_t write_domain)
  1236. {
  1237. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1238. int ret, i;
  1239. if (obj->read_domains & I915_GEM_DOMAIN_CPU)
  1240. return 0;
  1241. if (read_domains != I915_GEM_DOMAIN_CPU ||
  1242. write_domain != 0)
  1243. return i915_gem_object_set_domain(obj,
  1244. read_domains, write_domain);
  1245. /* Wait on any GPU rendering to the object to be flushed. */
  1246. if (obj->write_domain & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT)) {
  1247. ret = i915_gem_object_wait_rendering(obj);
  1248. if (ret)
  1249. return ret;
  1250. }
  1251. if (obj_priv->page_cpu_valid == NULL) {
  1252. obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
  1253. DRM_MEM_DRIVER);
  1254. }
  1255. /* Flush the cache on any pages that are still invalid from the CPU's
  1256. * perspective.
  1257. */
  1258. for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE; i++) {
  1259. if (obj_priv->page_cpu_valid[i])
  1260. continue;
  1261. drm_clflush_pages(obj_priv->page_list + i, 1);
  1262. obj_priv->page_cpu_valid[i] = 1;
  1263. }
  1264. return 0;
  1265. }
  1266. /**
  1267. * Once all of the objects have been set in the proper domain,
  1268. * perform the necessary flush and invalidate operations.
  1269. *
  1270. * Returns the write domains flushed, for use in flush tracking.
  1271. */
  1272. static uint32_t
  1273. i915_gem_dev_set_domain(struct drm_device *dev)
  1274. {
  1275. uint32_t flush_domains = dev->flush_domains;
  1276. /*
  1277. * Now that all the buffers are synced to the proper domains,
  1278. * flush and invalidate the collected domains
  1279. */
  1280. if (dev->invalidate_domains | dev->flush_domains) {
  1281. #if WATCH_EXEC
  1282. DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
  1283. __func__,
  1284. dev->invalidate_domains,
  1285. dev->flush_domains);
  1286. #endif
  1287. i915_gem_flush(dev,
  1288. dev->invalidate_domains,
  1289. dev->flush_domains);
  1290. dev->invalidate_domains = 0;
  1291. dev->flush_domains = 0;
  1292. }
  1293. return flush_domains;
  1294. }
  1295. /**
  1296. * Pin an object to the GTT and evaluate the relocations landing in it.
  1297. */
  1298. static int
  1299. i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
  1300. struct drm_file *file_priv,
  1301. struct drm_i915_gem_exec_object *entry)
  1302. {
  1303. struct drm_device *dev = obj->dev;
  1304. struct drm_i915_gem_relocation_entry reloc;
  1305. struct drm_i915_gem_relocation_entry __user *relocs;
  1306. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1307. int i, ret;
  1308. uint32_t last_reloc_offset = -1;
  1309. void __iomem *reloc_page = NULL;
  1310. /* Choose the GTT offset for our buffer and put it there. */
  1311. ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
  1312. if (ret)
  1313. return ret;
  1314. entry->offset = obj_priv->gtt_offset;
  1315. relocs = (struct drm_i915_gem_relocation_entry __user *)
  1316. (uintptr_t) entry->relocs_ptr;
  1317. /* Apply the relocations, using the GTT aperture to avoid cache
  1318. * flushing requirements.
  1319. */
  1320. for (i = 0; i < entry->relocation_count; i++) {
  1321. struct drm_gem_object *target_obj;
  1322. struct drm_i915_gem_object *target_obj_priv;
  1323. uint32_t reloc_val, reloc_offset;
  1324. uint32_t __iomem *reloc_entry;
  1325. ret = copy_from_user(&reloc, relocs + i, sizeof(reloc));
  1326. if (ret != 0) {
  1327. i915_gem_object_unpin(obj);
  1328. return ret;
  1329. }
  1330. target_obj = drm_gem_object_lookup(obj->dev, file_priv,
  1331. reloc.target_handle);
  1332. if (target_obj == NULL) {
  1333. i915_gem_object_unpin(obj);
  1334. return -EBADF;
  1335. }
  1336. target_obj_priv = target_obj->driver_private;
  1337. /* The target buffer should have appeared before us in the
  1338. * exec_object list, so it should have a GTT space bound by now.
  1339. */
  1340. if (target_obj_priv->gtt_space == NULL) {
  1341. DRM_ERROR("No GTT space found for object %d\n",
  1342. reloc.target_handle);
  1343. drm_gem_object_unreference(target_obj);
  1344. i915_gem_object_unpin(obj);
  1345. return -EINVAL;
  1346. }
  1347. if (reloc.offset > obj->size - 4) {
  1348. DRM_ERROR("Relocation beyond object bounds: "
  1349. "obj %p target %d offset %d size %d.\n",
  1350. obj, reloc.target_handle,
  1351. (int) reloc.offset, (int) obj->size);
  1352. drm_gem_object_unreference(target_obj);
  1353. i915_gem_object_unpin(obj);
  1354. return -EINVAL;
  1355. }
  1356. if (reloc.offset & 3) {
  1357. DRM_ERROR("Relocation not 4-byte aligned: "
  1358. "obj %p target %d offset %d.\n",
  1359. obj, reloc.target_handle,
  1360. (int) reloc.offset);
  1361. drm_gem_object_unreference(target_obj);
  1362. i915_gem_object_unpin(obj);
  1363. return -EINVAL;
  1364. }
  1365. if (reloc.write_domain && target_obj->pending_write_domain &&
  1366. reloc.write_domain != target_obj->pending_write_domain) {
  1367. DRM_ERROR("Write domain conflict: "
  1368. "obj %p target %d offset %d "
  1369. "new %08x old %08x\n",
  1370. obj, reloc.target_handle,
  1371. (int) reloc.offset,
  1372. reloc.write_domain,
  1373. target_obj->pending_write_domain);
  1374. drm_gem_object_unreference(target_obj);
  1375. i915_gem_object_unpin(obj);
  1376. return -EINVAL;
  1377. }
  1378. #if WATCH_RELOC
  1379. DRM_INFO("%s: obj %p offset %08x target %d "
  1380. "read %08x write %08x gtt %08x "
  1381. "presumed %08x delta %08x\n",
  1382. __func__,
  1383. obj,
  1384. (int) reloc.offset,
  1385. (int) reloc.target_handle,
  1386. (int) reloc.read_domains,
  1387. (int) reloc.write_domain,
  1388. (int) target_obj_priv->gtt_offset,
  1389. (int) reloc.presumed_offset,
  1390. reloc.delta);
  1391. #endif
  1392. target_obj->pending_read_domains |= reloc.read_domains;
  1393. target_obj->pending_write_domain |= reloc.write_domain;
  1394. /* If the relocation already has the right value in it, no
  1395. * more work needs to be done.
  1396. */
  1397. if (target_obj_priv->gtt_offset == reloc.presumed_offset) {
  1398. drm_gem_object_unreference(target_obj);
  1399. continue;
  1400. }
  1401. /* Now that we're going to actually write some data in,
  1402. * make sure that any rendering using this buffer's contents
  1403. * is completed.
  1404. */
  1405. i915_gem_object_wait_rendering(obj);
  1406. /* As we're writing through the gtt, flush
  1407. * any CPU writes before we write the relocations
  1408. */
  1409. if (obj->write_domain & I915_GEM_DOMAIN_CPU) {
  1410. i915_gem_clflush_object(obj);
  1411. drm_agp_chipset_flush(dev);
  1412. obj->write_domain = 0;
  1413. }
  1414. /* Map the page containing the relocation we're going to
  1415. * perform.
  1416. */
  1417. reloc_offset = obj_priv->gtt_offset + reloc.offset;
  1418. if (reloc_page == NULL ||
  1419. (last_reloc_offset & ~(PAGE_SIZE - 1)) !=
  1420. (reloc_offset & ~(PAGE_SIZE - 1))) {
  1421. if (reloc_page != NULL)
  1422. iounmap(reloc_page);
  1423. reloc_page = ioremap_wc(dev->agp->base +
  1424. (reloc_offset &
  1425. ~(PAGE_SIZE - 1)),
  1426. PAGE_SIZE);
  1427. last_reloc_offset = reloc_offset;
  1428. if (reloc_page == NULL) {
  1429. drm_gem_object_unreference(target_obj);
  1430. i915_gem_object_unpin(obj);
  1431. return -ENOMEM;
  1432. }
  1433. }
  1434. reloc_entry = (uint32_t __iomem *)(reloc_page +
  1435. (reloc_offset & (PAGE_SIZE - 1)));
  1436. reloc_val = target_obj_priv->gtt_offset + reloc.delta;
  1437. #if WATCH_BUF
  1438. DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
  1439. obj, (unsigned int) reloc.offset,
  1440. readl(reloc_entry), reloc_val);
  1441. #endif
  1442. writel(reloc_val, reloc_entry);
  1443. /* Write the updated presumed offset for this entry back out
  1444. * to the user.
  1445. */
  1446. reloc.presumed_offset = target_obj_priv->gtt_offset;
  1447. ret = copy_to_user(relocs + i, &reloc, sizeof(reloc));
  1448. if (ret != 0) {
  1449. drm_gem_object_unreference(target_obj);
  1450. i915_gem_object_unpin(obj);
  1451. return ret;
  1452. }
  1453. drm_gem_object_unreference(target_obj);
  1454. }
  1455. if (reloc_page != NULL)
  1456. iounmap(reloc_page);
  1457. #if WATCH_BUF
  1458. if (0)
  1459. i915_gem_dump_object(obj, 128, __func__, ~0);
  1460. #endif
  1461. return 0;
  1462. }
  1463. /** Dispatch a batchbuffer to the ring
  1464. */
  1465. static int
  1466. i915_dispatch_gem_execbuffer(struct drm_device *dev,
  1467. struct drm_i915_gem_execbuffer *exec,
  1468. uint64_t exec_offset)
  1469. {
  1470. drm_i915_private_t *dev_priv = dev->dev_private;
  1471. struct drm_clip_rect __user *boxes = (struct drm_clip_rect __user *)
  1472. (uintptr_t) exec->cliprects_ptr;
  1473. int nbox = exec->num_cliprects;
  1474. int i = 0, count;
  1475. uint32_t exec_start, exec_len;
  1476. RING_LOCALS;
  1477. exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
  1478. exec_len = (uint32_t) exec->batch_len;
  1479. if ((exec_start | exec_len) & 0x7) {
  1480. DRM_ERROR("alignment\n");
  1481. return -EINVAL;
  1482. }
  1483. if (!exec_start)
  1484. return -EINVAL;
  1485. count = nbox ? nbox : 1;
  1486. for (i = 0; i < count; i++) {
  1487. if (i < nbox) {
  1488. int ret = i915_emit_box(dev, boxes, i,
  1489. exec->DR1, exec->DR4);
  1490. if (ret)
  1491. return ret;
  1492. }
  1493. if (IS_I830(dev) || IS_845G(dev)) {
  1494. BEGIN_LP_RING(4);
  1495. OUT_RING(MI_BATCH_BUFFER);
  1496. OUT_RING(exec_start | MI_BATCH_NON_SECURE);
  1497. OUT_RING(exec_start + exec_len - 4);
  1498. OUT_RING(0);
  1499. ADVANCE_LP_RING();
  1500. } else {
  1501. BEGIN_LP_RING(2);
  1502. if (IS_I965G(dev)) {
  1503. OUT_RING(MI_BATCH_BUFFER_START |
  1504. (2 << 6) |
  1505. MI_BATCH_NON_SECURE_I965);
  1506. OUT_RING(exec_start);
  1507. } else {
  1508. OUT_RING(MI_BATCH_BUFFER_START |
  1509. (2 << 6));
  1510. OUT_RING(exec_start | MI_BATCH_NON_SECURE);
  1511. }
  1512. ADVANCE_LP_RING();
  1513. }
  1514. }
  1515. /* XXX breadcrumb */
  1516. return 0;
  1517. }
  1518. /* Throttle our rendering by waiting until the ring has completed our requests
  1519. * emitted over 20 msec ago.
  1520. *
  1521. * This should get us reasonable parallelism between CPU and GPU but also
  1522. * relatively low latency when blocking on a particular request to finish.
  1523. */
  1524. static int
  1525. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
  1526. {
  1527. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  1528. int ret = 0;
  1529. uint32_t seqno;
  1530. mutex_lock(&dev->struct_mutex);
  1531. seqno = i915_file_priv->mm.last_gem_throttle_seqno;
  1532. i915_file_priv->mm.last_gem_throttle_seqno =
  1533. i915_file_priv->mm.last_gem_seqno;
  1534. if (seqno)
  1535. ret = i915_wait_request(dev, seqno);
  1536. mutex_unlock(&dev->struct_mutex);
  1537. return ret;
  1538. }
  1539. int
  1540. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1541. struct drm_file *file_priv)
  1542. {
  1543. drm_i915_private_t *dev_priv = dev->dev_private;
  1544. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  1545. struct drm_i915_gem_execbuffer *args = data;
  1546. struct drm_i915_gem_exec_object *exec_list = NULL;
  1547. struct drm_gem_object **object_list = NULL;
  1548. struct drm_gem_object *batch_obj;
  1549. int ret, i, pinned = 0;
  1550. uint64_t exec_offset;
  1551. uint32_t seqno, flush_domains;
  1552. #if WATCH_EXEC
  1553. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  1554. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  1555. #endif
  1556. if (args->buffer_count < 1) {
  1557. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  1558. return -EINVAL;
  1559. }
  1560. /* Copy in the exec list from userland */
  1561. exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count,
  1562. DRM_MEM_DRIVER);
  1563. object_list = drm_calloc(sizeof(*object_list), args->buffer_count,
  1564. DRM_MEM_DRIVER);
  1565. if (exec_list == NULL || object_list == NULL) {
  1566. DRM_ERROR("Failed to allocate exec or object list "
  1567. "for %d buffers\n",
  1568. args->buffer_count);
  1569. ret = -ENOMEM;
  1570. goto pre_mutex_err;
  1571. }
  1572. ret = copy_from_user(exec_list,
  1573. (struct drm_i915_relocation_entry __user *)
  1574. (uintptr_t) args->buffers_ptr,
  1575. sizeof(*exec_list) * args->buffer_count);
  1576. if (ret != 0) {
  1577. DRM_ERROR("copy %d exec entries failed %d\n",
  1578. args->buffer_count, ret);
  1579. goto pre_mutex_err;
  1580. }
  1581. mutex_lock(&dev->struct_mutex);
  1582. i915_verify_inactive(dev, __FILE__, __LINE__);
  1583. if (dev_priv->mm.wedged) {
  1584. DRM_ERROR("Execbuf while wedged\n");
  1585. mutex_unlock(&dev->struct_mutex);
  1586. return -EIO;
  1587. }
  1588. if (dev_priv->mm.suspended) {
  1589. DRM_ERROR("Execbuf while VT-switched.\n");
  1590. mutex_unlock(&dev->struct_mutex);
  1591. return -EBUSY;
  1592. }
  1593. /* Zero the gloabl flush/invalidate flags. These
  1594. * will be modified as each object is bound to the
  1595. * gtt
  1596. */
  1597. dev->invalidate_domains = 0;
  1598. dev->flush_domains = 0;
  1599. /* Look up object handles and perform the relocations */
  1600. for (i = 0; i < args->buffer_count; i++) {
  1601. object_list[i] = drm_gem_object_lookup(dev, file_priv,
  1602. exec_list[i].handle);
  1603. if (object_list[i] == NULL) {
  1604. DRM_ERROR("Invalid object handle %d at index %d\n",
  1605. exec_list[i].handle, i);
  1606. ret = -EBADF;
  1607. goto err;
  1608. }
  1609. object_list[i]->pending_read_domains = 0;
  1610. object_list[i]->pending_write_domain = 0;
  1611. ret = i915_gem_object_pin_and_relocate(object_list[i],
  1612. file_priv,
  1613. &exec_list[i]);
  1614. if (ret) {
  1615. DRM_ERROR("object bind and relocate failed %d\n", ret);
  1616. goto err;
  1617. }
  1618. pinned = i + 1;
  1619. }
  1620. /* Set the pending read domains for the batch buffer to COMMAND */
  1621. batch_obj = object_list[args->buffer_count-1];
  1622. batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND;
  1623. batch_obj->pending_write_domain = 0;
  1624. i915_verify_inactive(dev, __FILE__, __LINE__);
  1625. for (i = 0; i < args->buffer_count; i++) {
  1626. struct drm_gem_object *obj = object_list[i];
  1627. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1628. if (obj_priv->gtt_space == NULL) {
  1629. /* We evicted the buffer in the process of validating
  1630. * our set of buffers in. We could try to recover by
  1631. * kicking them everything out and trying again from
  1632. * the start.
  1633. */
  1634. ret = -ENOMEM;
  1635. goto err;
  1636. }
  1637. /* make sure all previous memory operations have passed */
  1638. ret = i915_gem_object_set_domain(obj,
  1639. obj->pending_read_domains,
  1640. obj->pending_write_domain);
  1641. if (ret)
  1642. goto err;
  1643. }
  1644. i915_verify_inactive(dev, __FILE__, __LINE__);
  1645. /* Flush/invalidate caches and chipset buffer */
  1646. flush_domains = i915_gem_dev_set_domain(dev);
  1647. i915_verify_inactive(dev, __FILE__, __LINE__);
  1648. #if WATCH_COHERENCY
  1649. for (i = 0; i < args->buffer_count; i++) {
  1650. i915_gem_object_check_coherency(object_list[i],
  1651. exec_list[i].handle);
  1652. }
  1653. #endif
  1654. exec_offset = exec_list[args->buffer_count - 1].offset;
  1655. #if WATCH_EXEC
  1656. i915_gem_dump_object(object_list[args->buffer_count - 1],
  1657. args->batch_len,
  1658. __func__,
  1659. ~0);
  1660. #endif
  1661. (void)i915_add_request(dev, flush_domains);
  1662. /* Exec the batchbuffer */
  1663. ret = i915_dispatch_gem_execbuffer(dev, args, exec_offset);
  1664. if (ret) {
  1665. DRM_ERROR("dispatch failed %d\n", ret);
  1666. goto err;
  1667. }
  1668. /*
  1669. * Ensure that the commands in the batch buffer are
  1670. * finished before the interrupt fires
  1671. */
  1672. flush_domains = i915_retire_commands(dev);
  1673. i915_verify_inactive(dev, __FILE__, __LINE__);
  1674. /*
  1675. * Get a seqno representing the execution of the current buffer,
  1676. * which we can wait on. We would like to mitigate these interrupts,
  1677. * likely by only creating seqnos occasionally (so that we have
  1678. * *some* interrupts representing completion of buffers that we can
  1679. * wait on when trying to clear up gtt space).
  1680. */
  1681. seqno = i915_add_request(dev, flush_domains);
  1682. BUG_ON(seqno == 0);
  1683. i915_file_priv->mm.last_gem_seqno = seqno;
  1684. for (i = 0; i < args->buffer_count; i++) {
  1685. struct drm_gem_object *obj = object_list[i];
  1686. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1687. i915_gem_object_move_to_active(obj);
  1688. obj_priv->last_rendering_seqno = seqno;
  1689. #if WATCH_LRU
  1690. DRM_INFO("%s: move to exec list %p\n", __func__, obj);
  1691. #endif
  1692. }
  1693. #if WATCH_LRU
  1694. i915_dump_lru(dev, __func__);
  1695. #endif
  1696. i915_verify_inactive(dev, __FILE__, __LINE__);
  1697. /* Copy the new buffer offsets back to the user's exec list. */
  1698. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  1699. (uintptr_t) args->buffers_ptr,
  1700. exec_list,
  1701. sizeof(*exec_list) * args->buffer_count);
  1702. if (ret)
  1703. DRM_ERROR("failed to copy %d exec entries "
  1704. "back to user (%d)\n",
  1705. args->buffer_count, ret);
  1706. err:
  1707. if (object_list != NULL) {
  1708. for (i = 0; i < pinned; i++)
  1709. i915_gem_object_unpin(object_list[i]);
  1710. for (i = 0; i < args->buffer_count; i++)
  1711. drm_gem_object_unreference(object_list[i]);
  1712. }
  1713. mutex_unlock(&dev->struct_mutex);
  1714. pre_mutex_err:
  1715. drm_free(object_list, sizeof(*object_list) * args->buffer_count,
  1716. DRM_MEM_DRIVER);
  1717. drm_free(exec_list, sizeof(*exec_list) * args->buffer_count,
  1718. DRM_MEM_DRIVER);
  1719. return ret;
  1720. }
  1721. int
  1722. i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
  1723. {
  1724. struct drm_device *dev = obj->dev;
  1725. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1726. int ret;
  1727. i915_verify_inactive(dev, __FILE__, __LINE__);
  1728. if (obj_priv->gtt_space == NULL) {
  1729. ret = i915_gem_object_bind_to_gtt(obj, alignment);
  1730. if (ret != 0) {
  1731. DRM_ERROR("Failure to bind: %d", ret);
  1732. return ret;
  1733. }
  1734. }
  1735. obj_priv->pin_count++;
  1736. /* If the object is not active and not pending a flush,
  1737. * remove it from the inactive list
  1738. */
  1739. if (obj_priv->pin_count == 1) {
  1740. atomic_inc(&dev->pin_count);
  1741. atomic_add(obj->size, &dev->pin_memory);
  1742. if (!obj_priv->active &&
  1743. (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
  1744. I915_GEM_DOMAIN_GTT)) == 0 &&
  1745. !list_empty(&obj_priv->list))
  1746. list_del_init(&obj_priv->list);
  1747. }
  1748. i915_verify_inactive(dev, __FILE__, __LINE__);
  1749. return 0;
  1750. }
  1751. void
  1752. i915_gem_object_unpin(struct drm_gem_object *obj)
  1753. {
  1754. struct drm_device *dev = obj->dev;
  1755. drm_i915_private_t *dev_priv = dev->dev_private;
  1756. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1757. i915_verify_inactive(dev, __FILE__, __LINE__);
  1758. obj_priv->pin_count--;
  1759. BUG_ON(obj_priv->pin_count < 0);
  1760. BUG_ON(obj_priv->gtt_space == NULL);
  1761. /* If the object is no longer pinned, and is
  1762. * neither active nor being flushed, then stick it on
  1763. * the inactive list
  1764. */
  1765. if (obj_priv->pin_count == 0) {
  1766. if (!obj_priv->active &&
  1767. (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
  1768. I915_GEM_DOMAIN_GTT)) == 0)
  1769. list_move_tail(&obj_priv->list,
  1770. &dev_priv->mm.inactive_list);
  1771. atomic_dec(&dev->pin_count);
  1772. atomic_sub(obj->size, &dev->pin_memory);
  1773. }
  1774. i915_verify_inactive(dev, __FILE__, __LINE__);
  1775. }
  1776. int
  1777. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  1778. struct drm_file *file_priv)
  1779. {
  1780. struct drm_i915_gem_pin *args = data;
  1781. struct drm_gem_object *obj;
  1782. struct drm_i915_gem_object *obj_priv;
  1783. int ret;
  1784. mutex_lock(&dev->struct_mutex);
  1785. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1786. if (obj == NULL) {
  1787. DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
  1788. args->handle);
  1789. mutex_unlock(&dev->struct_mutex);
  1790. return -EBADF;
  1791. }
  1792. obj_priv = obj->driver_private;
  1793. ret = i915_gem_object_pin(obj, args->alignment);
  1794. if (ret != 0) {
  1795. drm_gem_object_unreference(obj);
  1796. mutex_unlock(&dev->struct_mutex);
  1797. return ret;
  1798. }
  1799. /* XXX - flush the CPU caches for pinned objects
  1800. * as the X server doesn't manage domains yet
  1801. */
  1802. if (obj->write_domain & I915_GEM_DOMAIN_CPU) {
  1803. i915_gem_clflush_object(obj);
  1804. drm_agp_chipset_flush(dev);
  1805. obj->write_domain = 0;
  1806. }
  1807. args->offset = obj_priv->gtt_offset;
  1808. drm_gem_object_unreference(obj);
  1809. mutex_unlock(&dev->struct_mutex);
  1810. return 0;
  1811. }
  1812. int
  1813. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  1814. struct drm_file *file_priv)
  1815. {
  1816. struct drm_i915_gem_pin *args = data;
  1817. struct drm_gem_object *obj;
  1818. mutex_lock(&dev->struct_mutex);
  1819. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1820. if (obj == NULL) {
  1821. DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
  1822. args->handle);
  1823. mutex_unlock(&dev->struct_mutex);
  1824. return -EBADF;
  1825. }
  1826. i915_gem_object_unpin(obj);
  1827. drm_gem_object_unreference(obj);
  1828. mutex_unlock(&dev->struct_mutex);
  1829. return 0;
  1830. }
  1831. int
  1832. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  1833. struct drm_file *file_priv)
  1834. {
  1835. struct drm_i915_gem_busy *args = data;
  1836. struct drm_gem_object *obj;
  1837. struct drm_i915_gem_object *obj_priv;
  1838. mutex_lock(&dev->struct_mutex);
  1839. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1840. if (obj == NULL) {
  1841. DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
  1842. args->handle);
  1843. mutex_unlock(&dev->struct_mutex);
  1844. return -EBADF;
  1845. }
  1846. obj_priv = obj->driver_private;
  1847. args->busy = obj_priv->active;
  1848. drm_gem_object_unreference(obj);
  1849. mutex_unlock(&dev->struct_mutex);
  1850. return 0;
  1851. }
  1852. int
  1853. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  1854. struct drm_file *file_priv)
  1855. {
  1856. return i915_gem_ring_throttle(dev, file_priv);
  1857. }
  1858. int i915_gem_init_object(struct drm_gem_object *obj)
  1859. {
  1860. struct drm_i915_gem_object *obj_priv;
  1861. obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
  1862. if (obj_priv == NULL)
  1863. return -ENOMEM;
  1864. /*
  1865. * We've just allocated pages from the kernel,
  1866. * so they've just been written by the CPU with
  1867. * zeros. They'll need to be clflushed before we
  1868. * use them with the GPU.
  1869. */
  1870. obj->write_domain = I915_GEM_DOMAIN_CPU;
  1871. obj->read_domains = I915_GEM_DOMAIN_CPU;
  1872. obj_priv->agp_type = AGP_USER_MEMORY;
  1873. obj->driver_private = obj_priv;
  1874. obj_priv->obj = obj;
  1875. INIT_LIST_HEAD(&obj_priv->list);
  1876. return 0;
  1877. }
  1878. void i915_gem_free_object(struct drm_gem_object *obj)
  1879. {
  1880. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1881. while (obj_priv->pin_count > 0)
  1882. i915_gem_object_unpin(obj);
  1883. i915_gem_object_unbind(obj);
  1884. drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
  1885. drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
  1886. }
  1887. static int
  1888. i915_gem_set_domain(struct drm_gem_object *obj,
  1889. struct drm_file *file_priv,
  1890. uint32_t read_domains,
  1891. uint32_t write_domain)
  1892. {
  1893. struct drm_device *dev = obj->dev;
  1894. int ret;
  1895. uint32_t flush_domains;
  1896. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1897. ret = i915_gem_object_set_domain(obj, read_domains, write_domain);
  1898. if (ret)
  1899. return ret;
  1900. flush_domains = i915_gem_dev_set_domain(obj->dev);
  1901. if (flush_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT))
  1902. (void) i915_add_request(dev, flush_domains);
  1903. return 0;
  1904. }
  1905. /** Unbinds all objects that are on the given buffer list. */
  1906. static int
  1907. i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
  1908. {
  1909. struct drm_gem_object *obj;
  1910. struct drm_i915_gem_object *obj_priv;
  1911. int ret;
  1912. while (!list_empty(head)) {
  1913. obj_priv = list_first_entry(head,
  1914. struct drm_i915_gem_object,
  1915. list);
  1916. obj = obj_priv->obj;
  1917. if (obj_priv->pin_count != 0) {
  1918. DRM_ERROR("Pinned object in unbind list\n");
  1919. mutex_unlock(&dev->struct_mutex);
  1920. return -EINVAL;
  1921. }
  1922. ret = i915_gem_object_unbind(obj);
  1923. if (ret != 0) {
  1924. DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
  1925. ret);
  1926. mutex_unlock(&dev->struct_mutex);
  1927. return ret;
  1928. }
  1929. }
  1930. return 0;
  1931. }
  1932. static int
  1933. i915_gem_idle(struct drm_device *dev)
  1934. {
  1935. drm_i915_private_t *dev_priv = dev->dev_private;
  1936. uint32_t seqno, cur_seqno, last_seqno;
  1937. int stuck, ret;
  1938. if (dev_priv->mm.suspended)
  1939. return 0;
  1940. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  1941. * We need to replace this with a semaphore, or something.
  1942. */
  1943. dev_priv->mm.suspended = 1;
  1944. i915_kernel_lost_context(dev);
  1945. /* Flush the GPU along with all non-CPU write domains
  1946. */
  1947. i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT),
  1948. ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  1949. seqno = i915_add_request(dev, ~(I915_GEM_DOMAIN_CPU |
  1950. I915_GEM_DOMAIN_GTT));
  1951. if (seqno == 0) {
  1952. mutex_unlock(&dev->struct_mutex);
  1953. return -ENOMEM;
  1954. }
  1955. dev_priv->mm.waiting_gem_seqno = seqno;
  1956. last_seqno = 0;
  1957. stuck = 0;
  1958. for (;;) {
  1959. cur_seqno = i915_get_gem_seqno(dev);
  1960. if (i915_seqno_passed(cur_seqno, seqno))
  1961. break;
  1962. if (last_seqno == cur_seqno) {
  1963. if (stuck++ > 100) {
  1964. DRM_ERROR("hardware wedged\n");
  1965. dev_priv->mm.wedged = 1;
  1966. DRM_WAKEUP(&dev_priv->irq_queue);
  1967. break;
  1968. }
  1969. }
  1970. msleep(10);
  1971. last_seqno = cur_seqno;
  1972. }
  1973. dev_priv->mm.waiting_gem_seqno = 0;
  1974. i915_gem_retire_requests(dev);
  1975. /* Active and flushing should now be empty as we've
  1976. * waited for a sequence higher than any pending execbuffer
  1977. */
  1978. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  1979. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  1980. /* Request should now be empty as we've also waited
  1981. * for the last request in the list
  1982. */
  1983. BUG_ON(!list_empty(&dev_priv->mm.request_list));
  1984. /* Move all buffers out of the GTT. */
  1985. ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
  1986. if (ret)
  1987. return ret;
  1988. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  1989. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  1990. BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
  1991. BUG_ON(!list_empty(&dev_priv->mm.request_list));
  1992. return 0;
  1993. }
  1994. static int
  1995. i915_gem_init_hws(struct drm_device *dev)
  1996. {
  1997. drm_i915_private_t *dev_priv = dev->dev_private;
  1998. struct drm_gem_object *obj;
  1999. struct drm_i915_gem_object *obj_priv;
  2000. int ret;
  2001. /* If we need a physical address for the status page, it's already
  2002. * initialized at driver load time.
  2003. */
  2004. if (!I915_NEED_GFX_HWS(dev))
  2005. return 0;
  2006. obj = drm_gem_object_alloc(dev, 4096);
  2007. if (obj == NULL) {
  2008. DRM_ERROR("Failed to allocate status page\n");
  2009. return -ENOMEM;
  2010. }
  2011. obj_priv = obj->driver_private;
  2012. obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
  2013. ret = i915_gem_object_pin(obj, 4096);
  2014. if (ret != 0) {
  2015. drm_gem_object_unreference(obj);
  2016. return ret;
  2017. }
  2018. dev_priv->status_gfx_addr = obj_priv->gtt_offset;
  2019. dev_priv->hw_status_page = kmap(obj_priv->page_list[0]);
  2020. if (dev_priv->hw_status_page == NULL) {
  2021. DRM_ERROR("Failed to map status page.\n");
  2022. memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
  2023. drm_gem_object_unreference(obj);
  2024. return -EINVAL;
  2025. }
  2026. dev_priv->hws_obj = obj;
  2027. memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
  2028. I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
  2029. I915_READ(HWS_PGA); /* posting read */
  2030. DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
  2031. return 0;
  2032. }
  2033. static int
  2034. i915_gem_init_ringbuffer(struct drm_device *dev)
  2035. {
  2036. drm_i915_private_t *dev_priv = dev->dev_private;
  2037. struct drm_gem_object *obj;
  2038. struct drm_i915_gem_object *obj_priv;
  2039. int ret;
  2040. u32 head;
  2041. ret = i915_gem_init_hws(dev);
  2042. if (ret != 0)
  2043. return ret;
  2044. obj = drm_gem_object_alloc(dev, 128 * 1024);
  2045. if (obj == NULL) {
  2046. DRM_ERROR("Failed to allocate ringbuffer\n");
  2047. return -ENOMEM;
  2048. }
  2049. obj_priv = obj->driver_private;
  2050. ret = i915_gem_object_pin(obj, 4096);
  2051. if (ret != 0) {
  2052. drm_gem_object_unreference(obj);
  2053. return ret;
  2054. }
  2055. /* Set up the kernel mapping for the ring. */
  2056. dev_priv->ring.Size = obj->size;
  2057. dev_priv->ring.tail_mask = obj->size - 1;
  2058. dev_priv->ring.map.offset = dev->agp->base + obj_priv->gtt_offset;
  2059. dev_priv->ring.map.size = obj->size;
  2060. dev_priv->ring.map.type = 0;
  2061. dev_priv->ring.map.flags = 0;
  2062. dev_priv->ring.map.mtrr = 0;
  2063. drm_core_ioremap_wc(&dev_priv->ring.map, dev);
  2064. if (dev_priv->ring.map.handle == NULL) {
  2065. DRM_ERROR("Failed to map ringbuffer.\n");
  2066. memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
  2067. drm_gem_object_unreference(obj);
  2068. return -EINVAL;
  2069. }
  2070. dev_priv->ring.ring_obj = obj;
  2071. dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
  2072. /* Stop the ring if it's running. */
  2073. I915_WRITE(PRB0_CTL, 0);
  2074. I915_WRITE(PRB0_TAIL, 0);
  2075. I915_WRITE(PRB0_HEAD, 0);
  2076. /* Initialize the ring. */
  2077. I915_WRITE(PRB0_START, obj_priv->gtt_offset);
  2078. head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  2079. /* G45 ring initialization fails to reset head to zero */
  2080. if (head != 0) {
  2081. DRM_ERROR("Ring head not reset to zero "
  2082. "ctl %08x head %08x tail %08x start %08x\n",
  2083. I915_READ(PRB0_CTL),
  2084. I915_READ(PRB0_HEAD),
  2085. I915_READ(PRB0_TAIL),
  2086. I915_READ(PRB0_START));
  2087. I915_WRITE(PRB0_HEAD, 0);
  2088. DRM_ERROR("Ring head forced to zero "
  2089. "ctl %08x head %08x tail %08x start %08x\n",
  2090. I915_READ(PRB0_CTL),
  2091. I915_READ(PRB0_HEAD),
  2092. I915_READ(PRB0_TAIL),
  2093. I915_READ(PRB0_START));
  2094. }
  2095. I915_WRITE(PRB0_CTL,
  2096. ((obj->size - 4096) & RING_NR_PAGES) |
  2097. RING_NO_REPORT |
  2098. RING_VALID);
  2099. head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  2100. /* If the head is still not zero, the ring is dead */
  2101. if (head != 0) {
  2102. DRM_ERROR("Ring initialization failed "
  2103. "ctl %08x head %08x tail %08x start %08x\n",
  2104. I915_READ(PRB0_CTL),
  2105. I915_READ(PRB0_HEAD),
  2106. I915_READ(PRB0_TAIL),
  2107. I915_READ(PRB0_START));
  2108. return -EIO;
  2109. }
  2110. /* Update our cache of the ring state */
  2111. i915_kernel_lost_context(dev);
  2112. return 0;
  2113. }
  2114. static void
  2115. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  2116. {
  2117. drm_i915_private_t *dev_priv = dev->dev_private;
  2118. if (dev_priv->ring.ring_obj == NULL)
  2119. return;
  2120. drm_core_ioremapfree(&dev_priv->ring.map, dev);
  2121. i915_gem_object_unpin(dev_priv->ring.ring_obj);
  2122. drm_gem_object_unreference(dev_priv->ring.ring_obj);
  2123. dev_priv->ring.ring_obj = NULL;
  2124. memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
  2125. if (dev_priv->hws_obj != NULL) {
  2126. struct drm_gem_object *obj = dev_priv->hws_obj;
  2127. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2128. kunmap(obj_priv->page_list[0]);
  2129. i915_gem_object_unpin(obj);
  2130. drm_gem_object_unreference(obj);
  2131. dev_priv->hws_obj = NULL;
  2132. memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
  2133. dev_priv->hw_status_page = NULL;
  2134. /* Write high address into HWS_PGA when disabling. */
  2135. I915_WRITE(HWS_PGA, 0x1ffff000);
  2136. }
  2137. }
  2138. int
  2139. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  2140. struct drm_file *file_priv)
  2141. {
  2142. drm_i915_private_t *dev_priv = dev->dev_private;
  2143. int ret;
  2144. if (dev_priv->mm.wedged) {
  2145. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  2146. dev_priv->mm.wedged = 0;
  2147. }
  2148. ret = i915_gem_init_ringbuffer(dev);
  2149. if (ret != 0)
  2150. return ret;
  2151. mutex_lock(&dev->struct_mutex);
  2152. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  2153. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  2154. BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
  2155. BUG_ON(!list_empty(&dev_priv->mm.request_list));
  2156. dev_priv->mm.suspended = 0;
  2157. mutex_unlock(&dev->struct_mutex);
  2158. drm_irq_install(dev);
  2159. return 0;
  2160. }
  2161. int
  2162. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  2163. struct drm_file *file_priv)
  2164. {
  2165. int ret;
  2166. mutex_lock(&dev->struct_mutex);
  2167. ret = i915_gem_idle(dev);
  2168. if (ret == 0)
  2169. i915_gem_cleanup_ringbuffer(dev);
  2170. mutex_unlock(&dev->struct_mutex);
  2171. drm_irq_uninstall(dev);
  2172. return 0;
  2173. }
  2174. void
  2175. i915_gem_lastclose(struct drm_device *dev)
  2176. {
  2177. int ret;
  2178. drm_i915_private_t *dev_priv = dev->dev_private;
  2179. mutex_lock(&dev->struct_mutex);
  2180. if (dev_priv->ring.ring_obj != NULL) {
  2181. ret = i915_gem_idle(dev);
  2182. if (ret)
  2183. DRM_ERROR("failed to idle hardware: %d\n", ret);
  2184. i915_gem_cleanup_ringbuffer(dev);
  2185. }
  2186. mutex_unlock(&dev->struct_mutex);
  2187. }
  2188. void
  2189. i915_gem_load(struct drm_device *dev)
  2190. {
  2191. drm_i915_private_t *dev_priv = dev->dev_private;
  2192. INIT_LIST_HEAD(&dev_priv->mm.active_list);
  2193. INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
  2194. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  2195. INIT_LIST_HEAD(&dev_priv->mm.request_list);
  2196. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  2197. i915_gem_retire_work_handler);
  2198. INIT_WORK(&dev_priv->mm.vblank_work,
  2199. i915_gem_vblank_work_handler);
  2200. dev_priv->mm.next_gem_seqno = 1;
  2201. i915_gem_detect_bit_6_swizzle(dev);
  2202. }