i915_gem.c 66 KB

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