i915_gem.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507
  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. #include <linux/pci.h>
  33. #define I915_GEM_GPU_DOMAINS (~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
  34. static void
  35. i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
  36. uint32_t read_domains,
  37. uint32_t write_domain);
  38. static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
  39. static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
  40. static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
  41. static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
  42. int write);
  43. static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
  44. uint64_t offset,
  45. uint64_t size);
  46. static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
  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 int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
  51. unsigned alignment);
  52. static int i915_gem_object_get_fence_reg(struct drm_gem_object *obj, bool write);
  53. static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
  54. static int i915_gem_evict_something(struct drm_device *dev);
  55. static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  56. struct drm_i915_gem_pwrite *args,
  57. struct drm_file *file_priv);
  58. int i915_gem_do_init(struct drm_device *dev, unsigned long start,
  59. unsigned long end)
  60. {
  61. drm_i915_private_t *dev_priv = dev->dev_private;
  62. if (start >= end ||
  63. (start & (PAGE_SIZE - 1)) != 0 ||
  64. (end & (PAGE_SIZE - 1)) != 0) {
  65. return -EINVAL;
  66. }
  67. drm_mm_init(&dev_priv->mm.gtt_space, start,
  68. end - start);
  69. dev->gtt_total = (uint32_t) (end - start);
  70. return 0;
  71. }
  72. int
  73. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  74. struct drm_file *file_priv)
  75. {
  76. struct drm_i915_gem_init *args = data;
  77. int ret;
  78. mutex_lock(&dev->struct_mutex);
  79. ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
  80. mutex_unlock(&dev->struct_mutex);
  81. return ret;
  82. }
  83. int
  84. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  85. struct drm_file *file_priv)
  86. {
  87. struct drm_i915_gem_get_aperture *args = data;
  88. if (!(dev->driver->driver_features & DRIVER_GEM))
  89. return -ENODEV;
  90. args->aper_size = dev->gtt_total;
  91. args->aper_available_size = (args->aper_size -
  92. atomic_read(&dev->pin_memory));
  93. return 0;
  94. }
  95. /**
  96. * Creates a new mm object and returns a handle to it.
  97. */
  98. int
  99. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  100. struct drm_file *file_priv)
  101. {
  102. struct drm_i915_gem_create *args = data;
  103. struct drm_gem_object *obj;
  104. int handle, ret;
  105. args->size = roundup(args->size, PAGE_SIZE);
  106. /* Allocate the new object */
  107. obj = drm_gem_object_alloc(dev, args->size);
  108. if (obj == NULL)
  109. return -ENOMEM;
  110. ret = drm_gem_handle_create(file_priv, obj, &handle);
  111. mutex_lock(&dev->struct_mutex);
  112. drm_gem_object_handle_unreference(obj);
  113. mutex_unlock(&dev->struct_mutex);
  114. if (ret)
  115. return ret;
  116. args->handle = handle;
  117. return 0;
  118. }
  119. /**
  120. * Reads data from the object referenced by handle.
  121. *
  122. * On error, the contents of *data are undefined.
  123. */
  124. int
  125. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  126. struct drm_file *file_priv)
  127. {
  128. struct drm_i915_gem_pread *args = data;
  129. struct drm_gem_object *obj;
  130. struct drm_i915_gem_object *obj_priv;
  131. ssize_t read;
  132. loff_t offset;
  133. int ret;
  134. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  135. if (obj == NULL)
  136. return -EBADF;
  137. obj_priv = obj->driver_private;
  138. /* Bounds check source.
  139. *
  140. * XXX: This could use review for overflow issues...
  141. */
  142. if (args->offset > obj->size || args->size > obj->size ||
  143. args->offset + args->size > obj->size) {
  144. drm_gem_object_unreference(obj);
  145. return -EINVAL;
  146. }
  147. mutex_lock(&dev->struct_mutex);
  148. ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
  149. args->size);
  150. if (ret != 0) {
  151. drm_gem_object_unreference(obj);
  152. mutex_unlock(&dev->struct_mutex);
  153. return ret;
  154. }
  155. offset = args->offset;
  156. read = vfs_read(obj->filp, (char __user *)(uintptr_t)args->data_ptr,
  157. args->size, &offset);
  158. if (read != args->size) {
  159. drm_gem_object_unreference(obj);
  160. mutex_unlock(&dev->struct_mutex);
  161. if (read < 0)
  162. return read;
  163. else
  164. return -EINVAL;
  165. }
  166. drm_gem_object_unreference(obj);
  167. mutex_unlock(&dev->struct_mutex);
  168. return 0;
  169. }
  170. /* This is the fast write path which cannot handle
  171. * page faults in the source data
  172. */
  173. static inline int
  174. fast_user_write(struct io_mapping *mapping,
  175. loff_t page_base, int page_offset,
  176. char __user *user_data,
  177. int length)
  178. {
  179. char *vaddr_atomic;
  180. unsigned long unwritten;
  181. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  182. unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
  183. user_data, length);
  184. io_mapping_unmap_atomic(vaddr_atomic);
  185. if (unwritten)
  186. return -EFAULT;
  187. return 0;
  188. }
  189. /* Here's the write path which can sleep for
  190. * page faults
  191. */
  192. static inline int
  193. slow_user_write(struct io_mapping *mapping,
  194. loff_t page_base, int page_offset,
  195. char __user *user_data,
  196. int length)
  197. {
  198. char __iomem *vaddr;
  199. unsigned long unwritten;
  200. vaddr = io_mapping_map_wc(mapping, page_base);
  201. if (vaddr == NULL)
  202. return -EFAULT;
  203. unwritten = __copy_from_user(vaddr + page_offset,
  204. user_data, length);
  205. io_mapping_unmap(vaddr);
  206. if (unwritten)
  207. return -EFAULT;
  208. return 0;
  209. }
  210. static int
  211. i915_gem_gtt_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  212. struct drm_i915_gem_pwrite *args,
  213. struct drm_file *file_priv)
  214. {
  215. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  216. drm_i915_private_t *dev_priv = dev->dev_private;
  217. ssize_t remain;
  218. loff_t offset, page_base;
  219. char __user *user_data;
  220. int page_offset, page_length;
  221. int ret;
  222. user_data = (char __user *) (uintptr_t) args->data_ptr;
  223. remain = args->size;
  224. if (!access_ok(VERIFY_READ, user_data, remain))
  225. return -EFAULT;
  226. mutex_lock(&dev->struct_mutex);
  227. ret = i915_gem_object_pin(obj, 0);
  228. if (ret) {
  229. mutex_unlock(&dev->struct_mutex);
  230. return ret;
  231. }
  232. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  233. if (ret)
  234. goto fail;
  235. obj_priv = obj->driver_private;
  236. offset = obj_priv->gtt_offset + args->offset;
  237. obj_priv->dirty = 1;
  238. while (remain > 0) {
  239. /* Operation in this page
  240. *
  241. * page_base = page offset within aperture
  242. * page_offset = offset within page
  243. * page_length = bytes to copy for this page
  244. */
  245. page_base = (offset & ~(PAGE_SIZE-1));
  246. page_offset = offset & (PAGE_SIZE-1);
  247. page_length = remain;
  248. if ((page_offset + remain) > PAGE_SIZE)
  249. page_length = PAGE_SIZE - page_offset;
  250. ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
  251. page_offset, user_data, page_length);
  252. /* If we get a fault while copying data, then (presumably) our
  253. * source page isn't available. In this case, use the
  254. * non-atomic function
  255. */
  256. if (ret) {
  257. ret = slow_user_write (dev_priv->mm.gtt_mapping,
  258. page_base, page_offset,
  259. user_data, page_length);
  260. if (ret)
  261. goto fail;
  262. }
  263. remain -= page_length;
  264. user_data += page_length;
  265. offset += page_length;
  266. }
  267. fail:
  268. i915_gem_object_unpin(obj);
  269. mutex_unlock(&dev->struct_mutex);
  270. return ret;
  271. }
  272. static int
  273. i915_gem_shmem_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  274. struct drm_i915_gem_pwrite *args,
  275. struct drm_file *file_priv)
  276. {
  277. int ret;
  278. loff_t offset;
  279. ssize_t written;
  280. mutex_lock(&dev->struct_mutex);
  281. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  282. if (ret) {
  283. mutex_unlock(&dev->struct_mutex);
  284. return ret;
  285. }
  286. offset = args->offset;
  287. written = vfs_write(obj->filp,
  288. (char __user *)(uintptr_t) args->data_ptr,
  289. args->size, &offset);
  290. if (written != args->size) {
  291. mutex_unlock(&dev->struct_mutex);
  292. if (written < 0)
  293. return written;
  294. else
  295. return -EINVAL;
  296. }
  297. mutex_unlock(&dev->struct_mutex);
  298. return 0;
  299. }
  300. /**
  301. * Writes data to the object referenced by handle.
  302. *
  303. * On error, the contents of the buffer that were to be modified are undefined.
  304. */
  305. int
  306. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  307. struct drm_file *file_priv)
  308. {
  309. struct drm_i915_gem_pwrite *args = data;
  310. struct drm_gem_object *obj;
  311. struct drm_i915_gem_object *obj_priv;
  312. int ret = 0;
  313. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  314. if (obj == NULL)
  315. return -EBADF;
  316. obj_priv = obj->driver_private;
  317. /* Bounds check destination.
  318. *
  319. * XXX: This could use review for overflow issues...
  320. */
  321. if (args->offset > obj->size || args->size > obj->size ||
  322. args->offset + args->size > obj->size) {
  323. drm_gem_object_unreference(obj);
  324. return -EINVAL;
  325. }
  326. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  327. * it would end up going through the fenced access, and we'll get
  328. * different detiling behavior between reading and writing.
  329. * pread/pwrite currently are reading and writing from the CPU
  330. * perspective, requiring manual detiling by the client.
  331. */
  332. if (obj_priv->phys_obj)
  333. ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
  334. else if (obj_priv->tiling_mode == I915_TILING_NONE &&
  335. dev->gtt_total != 0)
  336. ret = i915_gem_gtt_pwrite(dev, obj, args, file_priv);
  337. else
  338. ret = i915_gem_shmem_pwrite(dev, obj, args, file_priv);
  339. #if WATCH_PWRITE
  340. if (ret)
  341. DRM_INFO("pwrite failed %d\n", ret);
  342. #endif
  343. drm_gem_object_unreference(obj);
  344. return ret;
  345. }
  346. /**
  347. * Called when user space prepares to use an object with the CPU, either
  348. * through the mmap ioctl's mapping or a GTT mapping.
  349. */
  350. int
  351. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  352. struct drm_file *file_priv)
  353. {
  354. struct drm_i915_gem_set_domain *args = data;
  355. struct drm_gem_object *obj;
  356. uint32_t read_domains = args->read_domains;
  357. uint32_t write_domain = args->write_domain;
  358. int ret;
  359. if (!(dev->driver->driver_features & DRIVER_GEM))
  360. return -ENODEV;
  361. /* Only handle setting domains to types used by the CPU. */
  362. if (write_domain & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
  363. return -EINVAL;
  364. if (read_domains & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
  365. return -EINVAL;
  366. /* Having something in the write domain implies it's in the read
  367. * domain, and only that read domain. Enforce that in the request.
  368. */
  369. if (write_domain != 0 && read_domains != write_domain)
  370. return -EINVAL;
  371. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  372. if (obj == NULL)
  373. return -EBADF;
  374. mutex_lock(&dev->struct_mutex);
  375. #if WATCH_BUF
  376. DRM_INFO("set_domain_ioctl %p(%d), %08x %08x\n",
  377. obj, obj->size, read_domains, write_domain);
  378. #endif
  379. if (read_domains & I915_GEM_DOMAIN_GTT) {
  380. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  381. /* Silently promote "you're not bound, there was nothing to do"
  382. * to success, since the client was just asking us to
  383. * make sure everything was done.
  384. */
  385. if (ret == -EINVAL)
  386. ret = 0;
  387. } else {
  388. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  389. }
  390. drm_gem_object_unreference(obj);
  391. mutex_unlock(&dev->struct_mutex);
  392. return ret;
  393. }
  394. /**
  395. * Called when user space has done writes to this buffer
  396. */
  397. int
  398. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  399. struct drm_file *file_priv)
  400. {
  401. struct drm_i915_gem_sw_finish *args = data;
  402. struct drm_gem_object *obj;
  403. struct drm_i915_gem_object *obj_priv;
  404. int ret = 0;
  405. if (!(dev->driver->driver_features & DRIVER_GEM))
  406. return -ENODEV;
  407. mutex_lock(&dev->struct_mutex);
  408. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  409. if (obj == NULL) {
  410. mutex_unlock(&dev->struct_mutex);
  411. return -EBADF;
  412. }
  413. #if WATCH_BUF
  414. DRM_INFO("%s: sw_finish %d (%p %d)\n",
  415. __func__, args->handle, obj, obj->size);
  416. #endif
  417. obj_priv = obj->driver_private;
  418. /* Pinned buffers may be scanout, so flush the cache */
  419. if (obj_priv->pin_count)
  420. i915_gem_object_flush_cpu_write_domain(obj);
  421. drm_gem_object_unreference(obj);
  422. mutex_unlock(&dev->struct_mutex);
  423. return ret;
  424. }
  425. /**
  426. * Maps the contents of an object, returning the address it is mapped
  427. * into.
  428. *
  429. * While the mapping holds a reference on the contents of the object, it doesn't
  430. * imply a ref on the object itself.
  431. */
  432. int
  433. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  434. struct drm_file *file_priv)
  435. {
  436. struct drm_i915_gem_mmap *args = data;
  437. struct drm_gem_object *obj;
  438. loff_t offset;
  439. unsigned long addr;
  440. if (!(dev->driver->driver_features & DRIVER_GEM))
  441. return -ENODEV;
  442. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  443. if (obj == NULL)
  444. return -EBADF;
  445. offset = args->offset;
  446. down_write(&current->mm->mmap_sem);
  447. addr = do_mmap(obj->filp, 0, args->size,
  448. PROT_READ | PROT_WRITE, MAP_SHARED,
  449. args->offset);
  450. up_write(&current->mm->mmap_sem);
  451. mutex_lock(&dev->struct_mutex);
  452. drm_gem_object_unreference(obj);
  453. mutex_unlock(&dev->struct_mutex);
  454. if (IS_ERR((void *)addr))
  455. return addr;
  456. args->addr_ptr = (uint64_t) addr;
  457. return 0;
  458. }
  459. /**
  460. * i915_gem_fault - fault a page into the GTT
  461. * vma: VMA in question
  462. * vmf: fault info
  463. *
  464. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  465. * from userspace. The fault handler takes care of binding the object to
  466. * the GTT (if needed), allocating and programming a fence register (again,
  467. * only if needed based on whether the old reg is still valid or the object
  468. * is tiled) and inserting a new PTE into the faulting process.
  469. *
  470. * Note that the faulting process may involve evicting existing objects
  471. * from the GTT and/or fence registers to make room. So performance may
  472. * suffer if the GTT working set is large or there are few fence registers
  473. * left.
  474. */
  475. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  476. {
  477. struct drm_gem_object *obj = vma->vm_private_data;
  478. struct drm_device *dev = obj->dev;
  479. struct drm_i915_private *dev_priv = dev->dev_private;
  480. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  481. pgoff_t page_offset;
  482. unsigned long pfn;
  483. int ret = 0;
  484. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  485. /* We don't use vmf->pgoff since that has the fake offset */
  486. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  487. PAGE_SHIFT;
  488. /* Now bind it into the GTT if needed */
  489. mutex_lock(&dev->struct_mutex);
  490. if (!obj_priv->gtt_space) {
  491. ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
  492. if (ret) {
  493. mutex_unlock(&dev->struct_mutex);
  494. return VM_FAULT_SIGBUS;
  495. }
  496. list_add(&obj_priv->list, &dev_priv->mm.inactive_list);
  497. }
  498. /* Need a new fence register? */
  499. if (obj_priv->fence_reg == I915_FENCE_REG_NONE &&
  500. obj_priv->tiling_mode != I915_TILING_NONE) {
  501. ret = i915_gem_object_get_fence_reg(obj, write);
  502. if (ret) {
  503. mutex_unlock(&dev->struct_mutex);
  504. return VM_FAULT_SIGBUS;
  505. }
  506. }
  507. pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
  508. page_offset;
  509. /* Finally, remap it using the new GTT offset */
  510. ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
  511. mutex_unlock(&dev->struct_mutex);
  512. switch (ret) {
  513. case -ENOMEM:
  514. case -EAGAIN:
  515. return VM_FAULT_OOM;
  516. case -EFAULT:
  517. case -EBUSY:
  518. DRM_ERROR("can't insert pfn?? fault or busy...\n");
  519. return VM_FAULT_SIGBUS;
  520. default:
  521. return VM_FAULT_NOPAGE;
  522. }
  523. }
  524. /**
  525. * i915_gem_create_mmap_offset - create a fake mmap offset for an object
  526. * @obj: obj in question
  527. *
  528. * GEM memory mapping works by handing back to userspace a fake mmap offset
  529. * it can use in a subsequent mmap(2) call. The DRM core code then looks
  530. * up the object based on the offset and sets up the various memory mapping
  531. * structures.
  532. *
  533. * This routine allocates and attaches a fake offset for @obj.
  534. */
  535. static int
  536. i915_gem_create_mmap_offset(struct drm_gem_object *obj)
  537. {
  538. struct drm_device *dev = obj->dev;
  539. struct drm_gem_mm *mm = dev->mm_private;
  540. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  541. struct drm_map_list *list;
  542. struct drm_map *map;
  543. int ret = 0;
  544. /* Set the object up for mmap'ing */
  545. list = &obj->map_list;
  546. list->map = drm_calloc(1, sizeof(struct drm_map_list),
  547. DRM_MEM_DRIVER);
  548. if (!list->map)
  549. return -ENOMEM;
  550. map = list->map;
  551. map->type = _DRM_GEM;
  552. map->size = obj->size;
  553. map->handle = obj;
  554. /* Get a DRM GEM mmap offset allocated... */
  555. list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
  556. obj->size / PAGE_SIZE, 0, 0);
  557. if (!list->file_offset_node) {
  558. DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
  559. ret = -ENOMEM;
  560. goto out_free_list;
  561. }
  562. list->file_offset_node = drm_mm_get_block(list->file_offset_node,
  563. obj->size / PAGE_SIZE, 0);
  564. if (!list->file_offset_node) {
  565. ret = -ENOMEM;
  566. goto out_free_list;
  567. }
  568. list->hash.key = list->file_offset_node->start;
  569. if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
  570. DRM_ERROR("failed to add to map hash\n");
  571. goto out_free_mm;
  572. }
  573. /* By now we should be all set, any drm_mmap request on the offset
  574. * below will get to our mmap & fault handler */
  575. obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
  576. return 0;
  577. out_free_mm:
  578. drm_mm_put_block(list->file_offset_node);
  579. out_free_list:
  580. drm_free(list->map, sizeof(struct drm_map_list), DRM_MEM_DRIVER);
  581. return ret;
  582. }
  583. /**
  584. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  585. * @obj: object to check
  586. *
  587. * Return the required GTT alignment for an object, taking into account
  588. * potential fence register mapping if needed.
  589. */
  590. static uint32_t
  591. i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
  592. {
  593. struct drm_device *dev = obj->dev;
  594. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  595. int start, i;
  596. /*
  597. * Minimum alignment is 4k (GTT page size), but might be greater
  598. * if a fence register is needed for the object.
  599. */
  600. if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
  601. return 4096;
  602. /*
  603. * Previous chips need to be aligned to the size of the smallest
  604. * fence register that can contain the object.
  605. */
  606. if (IS_I9XX(dev))
  607. start = 1024*1024;
  608. else
  609. start = 512*1024;
  610. for (i = start; i < obj->size; i <<= 1)
  611. ;
  612. return i;
  613. }
  614. /**
  615. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  616. * @dev: DRM device
  617. * @data: GTT mapping ioctl data
  618. * @file_priv: GEM object info
  619. *
  620. * Simply returns the fake offset to userspace so it can mmap it.
  621. * The mmap call will end up in drm_gem_mmap(), which will set things
  622. * up so we can get faults in the handler above.
  623. *
  624. * The fault handler will take care of binding the object into the GTT
  625. * (since it may have been evicted to make room for something), allocating
  626. * a fence register, and mapping the appropriate aperture address into
  627. * userspace.
  628. */
  629. int
  630. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  631. struct drm_file *file_priv)
  632. {
  633. struct drm_i915_gem_mmap_gtt *args = data;
  634. struct drm_i915_private *dev_priv = dev->dev_private;
  635. struct drm_gem_object *obj;
  636. struct drm_i915_gem_object *obj_priv;
  637. int ret;
  638. if (!(dev->driver->driver_features & DRIVER_GEM))
  639. return -ENODEV;
  640. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  641. if (obj == NULL)
  642. return -EBADF;
  643. mutex_lock(&dev->struct_mutex);
  644. obj_priv = obj->driver_private;
  645. if (!obj_priv->mmap_offset) {
  646. ret = i915_gem_create_mmap_offset(obj);
  647. if (ret)
  648. return ret;
  649. }
  650. args->offset = obj_priv->mmap_offset;
  651. obj_priv->gtt_alignment = i915_gem_get_gtt_alignment(obj);
  652. /* Make sure the alignment is correct for fence regs etc */
  653. if (obj_priv->agp_mem &&
  654. (obj_priv->gtt_offset & (obj_priv->gtt_alignment - 1))) {
  655. drm_gem_object_unreference(obj);
  656. mutex_unlock(&dev->struct_mutex);
  657. return -EINVAL;
  658. }
  659. /*
  660. * Pull it into the GTT so that we have a page list (makes the
  661. * initial fault faster and any subsequent flushing possible).
  662. */
  663. if (!obj_priv->agp_mem) {
  664. ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
  665. if (ret) {
  666. drm_gem_object_unreference(obj);
  667. mutex_unlock(&dev->struct_mutex);
  668. return ret;
  669. }
  670. list_add(&obj_priv->list, &dev_priv->mm.inactive_list);
  671. }
  672. drm_gem_object_unreference(obj);
  673. mutex_unlock(&dev->struct_mutex);
  674. return 0;
  675. }
  676. static void
  677. i915_gem_object_free_page_list(struct drm_gem_object *obj)
  678. {
  679. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  680. int page_count = obj->size / PAGE_SIZE;
  681. int i;
  682. if (obj_priv->page_list == NULL)
  683. return;
  684. for (i = 0; i < page_count; i++)
  685. if (obj_priv->page_list[i] != NULL) {
  686. if (obj_priv->dirty)
  687. set_page_dirty(obj_priv->page_list[i]);
  688. mark_page_accessed(obj_priv->page_list[i]);
  689. page_cache_release(obj_priv->page_list[i]);
  690. }
  691. obj_priv->dirty = 0;
  692. drm_free(obj_priv->page_list,
  693. page_count * sizeof(struct page *),
  694. DRM_MEM_DRIVER);
  695. obj_priv->page_list = NULL;
  696. }
  697. static void
  698. i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno)
  699. {
  700. struct drm_device *dev = obj->dev;
  701. drm_i915_private_t *dev_priv = dev->dev_private;
  702. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  703. /* Add a reference if we're newly entering the active list. */
  704. if (!obj_priv->active) {
  705. drm_gem_object_reference(obj);
  706. obj_priv->active = 1;
  707. }
  708. /* Move from whatever list we were on to the tail of execution. */
  709. list_move_tail(&obj_priv->list,
  710. &dev_priv->mm.active_list);
  711. obj_priv->last_rendering_seqno = seqno;
  712. }
  713. static void
  714. i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
  715. {
  716. struct drm_device *dev = obj->dev;
  717. drm_i915_private_t *dev_priv = dev->dev_private;
  718. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  719. BUG_ON(!obj_priv->active);
  720. list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
  721. obj_priv->last_rendering_seqno = 0;
  722. }
  723. static void
  724. i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
  725. {
  726. struct drm_device *dev = obj->dev;
  727. drm_i915_private_t *dev_priv = dev->dev_private;
  728. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  729. i915_verify_inactive(dev, __FILE__, __LINE__);
  730. if (obj_priv->pin_count != 0)
  731. list_del_init(&obj_priv->list);
  732. else
  733. list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
  734. obj_priv->last_rendering_seqno = 0;
  735. if (obj_priv->active) {
  736. obj_priv->active = 0;
  737. drm_gem_object_unreference(obj);
  738. }
  739. i915_verify_inactive(dev, __FILE__, __LINE__);
  740. }
  741. /**
  742. * Creates a new sequence number, emitting a write of it to the status page
  743. * plus an interrupt, which will trigger i915_user_interrupt_handler.
  744. *
  745. * Must be called with struct_lock held.
  746. *
  747. * Returned sequence numbers are nonzero on success.
  748. */
  749. static uint32_t
  750. i915_add_request(struct drm_device *dev, uint32_t flush_domains)
  751. {
  752. drm_i915_private_t *dev_priv = dev->dev_private;
  753. struct drm_i915_gem_request *request;
  754. uint32_t seqno;
  755. int was_empty;
  756. RING_LOCALS;
  757. request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER);
  758. if (request == NULL)
  759. return 0;
  760. /* Grab the seqno we're going to make this request be, and bump the
  761. * next (skipping 0 so it can be the reserved no-seqno value).
  762. */
  763. seqno = dev_priv->mm.next_gem_seqno;
  764. dev_priv->mm.next_gem_seqno++;
  765. if (dev_priv->mm.next_gem_seqno == 0)
  766. dev_priv->mm.next_gem_seqno++;
  767. BEGIN_LP_RING(4);
  768. OUT_RING(MI_STORE_DWORD_INDEX);
  769. OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
  770. OUT_RING(seqno);
  771. OUT_RING(MI_USER_INTERRUPT);
  772. ADVANCE_LP_RING();
  773. DRM_DEBUG("%d\n", seqno);
  774. request->seqno = seqno;
  775. request->emitted_jiffies = jiffies;
  776. was_empty = list_empty(&dev_priv->mm.request_list);
  777. list_add_tail(&request->list, &dev_priv->mm.request_list);
  778. /* Associate any objects on the flushing list matching the write
  779. * domain we're flushing with our flush.
  780. */
  781. if (flush_domains != 0) {
  782. struct drm_i915_gem_object *obj_priv, *next;
  783. list_for_each_entry_safe(obj_priv, next,
  784. &dev_priv->mm.flushing_list, list) {
  785. struct drm_gem_object *obj = obj_priv->obj;
  786. if ((obj->write_domain & flush_domains) ==
  787. obj->write_domain) {
  788. obj->write_domain = 0;
  789. i915_gem_object_move_to_active(obj, seqno);
  790. }
  791. }
  792. }
  793. if (was_empty && !dev_priv->mm.suspended)
  794. schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
  795. return seqno;
  796. }
  797. /**
  798. * Command execution barrier
  799. *
  800. * Ensures that all commands in the ring are finished
  801. * before signalling the CPU
  802. */
  803. static uint32_t
  804. i915_retire_commands(struct drm_device *dev)
  805. {
  806. drm_i915_private_t *dev_priv = dev->dev_private;
  807. uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
  808. uint32_t flush_domains = 0;
  809. RING_LOCALS;
  810. /* The sampler always gets flushed on i965 (sigh) */
  811. if (IS_I965G(dev))
  812. flush_domains |= I915_GEM_DOMAIN_SAMPLER;
  813. BEGIN_LP_RING(2);
  814. OUT_RING(cmd);
  815. OUT_RING(0); /* noop */
  816. ADVANCE_LP_RING();
  817. return flush_domains;
  818. }
  819. /**
  820. * Moves buffers associated only with the given active seqno from the active
  821. * to inactive list, potentially freeing them.
  822. */
  823. static void
  824. i915_gem_retire_request(struct drm_device *dev,
  825. struct drm_i915_gem_request *request)
  826. {
  827. drm_i915_private_t *dev_priv = dev->dev_private;
  828. /* Move any buffers on the active list that are no longer referenced
  829. * by the ringbuffer to the flushing/inactive lists as appropriate.
  830. */
  831. while (!list_empty(&dev_priv->mm.active_list)) {
  832. struct drm_gem_object *obj;
  833. struct drm_i915_gem_object *obj_priv;
  834. obj_priv = list_first_entry(&dev_priv->mm.active_list,
  835. struct drm_i915_gem_object,
  836. list);
  837. obj = obj_priv->obj;
  838. /* If the seqno being retired doesn't match the oldest in the
  839. * list, then the oldest in the list must still be newer than
  840. * this seqno.
  841. */
  842. if (obj_priv->last_rendering_seqno != request->seqno)
  843. return;
  844. #if WATCH_LRU
  845. DRM_INFO("%s: retire %d moves to inactive list %p\n",
  846. __func__, request->seqno, obj);
  847. #endif
  848. if (obj->write_domain != 0)
  849. i915_gem_object_move_to_flushing(obj);
  850. else
  851. i915_gem_object_move_to_inactive(obj);
  852. }
  853. }
  854. /**
  855. * Returns true if seq1 is later than seq2.
  856. */
  857. static int
  858. i915_seqno_passed(uint32_t seq1, uint32_t seq2)
  859. {
  860. return (int32_t)(seq1 - seq2) >= 0;
  861. }
  862. uint32_t
  863. i915_get_gem_seqno(struct drm_device *dev)
  864. {
  865. drm_i915_private_t *dev_priv = dev->dev_private;
  866. return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
  867. }
  868. /**
  869. * This function clears the request list as sequence numbers are passed.
  870. */
  871. void
  872. i915_gem_retire_requests(struct drm_device *dev)
  873. {
  874. drm_i915_private_t *dev_priv = dev->dev_private;
  875. uint32_t seqno;
  876. seqno = i915_get_gem_seqno(dev);
  877. while (!list_empty(&dev_priv->mm.request_list)) {
  878. struct drm_i915_gem_request *request;
  879. uint32_t retiring_seqno;
  880. request = list_first_entry(&dev_priv->mm.request_list,
  881. struct drm_i915_gem_request,
  882. list);
  883. retiring_seqno = request->seqno;
  884. if (i915_seqno_passed(seqno, retiring_seqno) ||
  885. dev_priv->mm.wedged) {
  886. i915_gem_retire_request(dev, request);
  887. list_del(&request->list);
  888. drm_free(request, sizeof(*request), DRM_MEM_DRIVER);
  889. } else
  890. break;
  891. }
  892. }
  893. void
  894. i915_gem_retire_work_handler(struct work_struct *work)
  895. {
  896. drm_i915_private_t *dev_priv;
  897. struct drm_device *dev;
  898. dev_priv = container_of(work, drm_i915_private_t,
  899. mm.retire_work.work);
  900. dev = dev_priv->dev;
  901. mutex_lock(&dev->struct_mutex);
  902. i915_gem_retire_requests(dev);
  903. if (!dev_priv->mm.suspended &&
  904. !list_empty(&dev_priv->mm.request_list))
  905. schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
  906. mutex_unlock(&dev->struct_mutex);
  907. }
  908. /**
  909. * Waits for a sequence number to be signaled, and cleans up the
  910. * request and object lists appropriately for that event.
  911. */
  912. static int
  913. i915_wait_request(struct drm_device *dev, uint32_t seqno)
  914. {
  915. drm_i915_private_t *dev_priv = dev->dev_private;
  916. int ret = 0;
  917. BUG_ON(seqno == 0);
  918. if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
  919. dev_priv->mm.waiting_gem_seqno = seqno;
  920. i915_user_irq_get(dev);
  921. ret = wait_event_interruptible(dev_priv->irq_queue,
  922. i915_seqno_passed(i915_get_gem_seqno(dev),
  923. seqno) ||
  924. dev_priv->mm.wedged);
  925. i915_user_irq_put(dev);
  926. dev_priv->mm.waiting_gem_seqno = 0;
  927. }
  928. if (dev_priv->mm.wedged)
  929. ret = -EIO;
  930. if (ret && ret != -ERESTARTSYS)
  931. DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
  932. __func__, ret, seqno, i915_get_gem_seqno(dev));
  933. /* Directly dispatch request retiring. While we have the work queue
  934. * to handle this, the waiter on a request often wants an associated
  935. * buffer to have made it to the inactive list, and we would need
  936. * a separate wait queue to handle that.
  937. */
  938. if (ret == 0)
  939. i915_gem_retire_requests(dev);
  940. return ret;
  941. }
  942. static void
  943. i915_gem_flush(struct drm_device *dev,
  944. uint32_t invalidate_domains,
  945. uint32_t flush_domains)
  946. {
  947. drm_i915_private_t *dev_priv = dev->dev_private;
  948. uint32_t cmd;
  949. RING_LOCALS;
  950. #if WATCH_EXEC
  951. DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
  952. invalidate_domains, flush_domains);
  953. #endif
  954. if (flush_domains & I915_GEM_DOMAIN_CPU)
  955. drm_agp_chipset_flush(dev);
  956. if ((invalidate_domains | flush_domains) & ~(I915_GEM_DOMAIN_CPU |
  957. I915_GEM_DOMAIN_GTT)) {
  958. /*
  959. * read/write caches:
  960. *
  961. * I915_GEM_DOMAIN_RENDER is always invalidated, but is
  962. * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
  963. * also flushed at 2d versus 3d pipeline switches.
  964. *
  965. * read-only caches:
  966. *
  967. * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
  968. * MI_READ_FLUSH is set, and is always flushed on 965.
  969. *
  970. * I915_GEM_DOMAIN_COMMAND may not exist?
  971. *
  972. * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
  973. * invalidated when MI_EXE_FLUSH is set.
  974. *
  975. * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
  976. * invalidated with every MI_FLUSH.
  977. *
  978. * TLBs:
  979. *
  980. * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
  981. * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
  982. * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
  983. * are flushed at any MI_FLUSH.
  984. */
  985. cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
  986. if ((invalidate_domains|flush_domains) &
  987. I915_GEM_DOMAIN_RENDER)
  988. cmd &= ~MI_NO_WRITE_FLUSH;
  989. if (!IS_I965G(dev)) {
  990. /*
  991. * On the 965, the sampler cache always gets flushed
  992. * and this bit is reserved.
  993. */
  994. if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
  995. cmd |= MI_READ_FLUSH;
  996. }
  997. if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
  998. cmd |= MI_EXE_FLUSH;
  999. #if WATCH_EXEC
  1000. DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
  1001. #endif
  1002. BEGIN_LP_RING(2);
  1003. OUT_RING(cmd);
  1004. OUT_RING(0); /* noop */
  1005. ADVANCE_LP_RING();
  1006. }
  1007. }
  1008. /**
  1009. * Ensures that all rendering to the object has completed and the object is
  1010. * safe to unbind from the GTT or access from the CPU.
  1011. */
  1012. static int
  1013. i915_gem_object_wait_rendering(struct drm_gem_object *obj)
  1014. {
  1015. struct drm_device *dev = obj->dev;
  1016. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1017. int ret;
  1018. /* This function only exists to support waiting for existing rendering,
  1019. * not for emitting required flushes.
  1020. */
  1021. BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
  1022. /* If there is rendering queued on the buffer being evicted, wait for
  1023. * it.
  1024. */
  1025. if (obj_priv->active) {
  1026. #if WATCH_BUF
  1027. DRM_INFO("%s: object %p wait for seqno %08x\n",
  1028. __func__, obj, obj_priv->last_rendering_seqno);
  1029. #endif
  1030. ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
  1031. if (ret != 0)
  1032. return ret;
  1033. }
  1034. return 0;
  1035. }
  1036. /**
  1037. * Unbinds an object from the GTT aperture.
  1038. */
  1039. int
  1040. i915_gem_object_unbind(struct drm_gem_object *obj)
  1041. {
  1042. struct drm_device *dev = obj->dev;
  1043. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1044. loff_t offset;
  1045. int ret = 0;
  1046. #if WATCH_BUF
  1047. DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
  1048. DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
  1049. #endif
  1050. if (obj_priv->gtt_space == NULL)
  1051. return 0;
  1052. if (obj_priv->pin_count != 0) {
  1053. DRM_ERROR("Attempting to unbind pinned buffer\n");
  1054. return -EINVAL;
  1055. }
  1056. /* Move the object to the CPU domain to ensure that
  1057. * any possible CPU writes while it's not in the GTT
  1058. * are flushed when we go to remap it. This will
  1059. * also ensure that all pending GPU writes are finished
  1060. * before we unbind.
  1061. */
  1062. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  1063. if (ret) {
  1064. if (ret != -ERESTARTSYS)
  1065. DRM_ERROR("set_domain failed: %d\n", ret);
  1066. return ret;
  1067. }
  1068. if (obj_priv->agp_mem != NULL) {
  1069. drm_unbind_agp(obj_priv->agp_mem);
  1070. drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
  1071. obj_priv->agp_mem = NULL;
  1072. }
  1073. BUG_ON(obj_priv->active);
  1074. /* blow away mappings if mapped through GTT */
  1075. offset = ((loff_t) obj->map_list.hash.key) << PAGE_SHIFT;
  1076. if (dev->dev_mapping)
  1077. unmap_mapping_range(dev->dev_mapping, offset, obj->size, 1);
  1078. if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
  1079. i915_gem_clear_fence_reg(obj);
  1080. i915_gem_object_free_page_list(obj);
  1081. if (obj_priv->gtt_space) {
  1082. atomic_dec(&dev->gtt_count);
  1083. atomic_sub(obj->size, &dev->gtt_memory);
  1084. drm_mm_put_block(obj_priv->gtt_space);
  1085. obj_priv->gtt_space = NULL;
  1086. }
  1087. /* Remove ourselves from the LRU list if present. */
  1088. if (!list_empty(&obj_priv->list))
  1089. list_del_init(&obj_priv->list);
  1090. return 0;
  1091. }
  1092. static int
  1093. i915_gem_evict_something(struct drm_device *dev)
  1094. {
  1095. drm_i915_private_t *dev_priv = dev->dev_private;
  1096. struct drm_gem_object *obj;
  1097. struct drm_i915_gem_object *obj_priv;
  1098. int ret = 0;
  1099. for (;;) {
  1100. /* If there's an inactive buffer available now, grab it
  1101. * and be done.
  1102. */
  1103. if (!list_empty(&dev_priv->mm.inactive_list)) {
  1104. obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
  1105. struct drm_i915_gem_object,
  1106. list);
  1107. obj = obj_priv->obj;
  1108. BUG_ON(obj_priv->pin_count != 0);
  1109. #if WATCH_LRU
  1110. DRM_INFO("%s: evicting %p\n", __func__, obj);
  1111. #endif
  1112. BUG_ON(obj_priv->active);
  1113. /* Wait on the rendering and unbind the buffer. */
  1114. ret = i915_gem_object_unbind(obj);
  1115. break;
  1116. }
  1117. /* If we didn't get anything, but the ring is still processing
  1118. * things, wait for one of those things to finish and hopefully
  1119. * leave us a buffer to evict.
  1120. */
  1121. if (!list_empty(&dev_priv->mm.request_list)) {
  1122. struct drm_i915_gem_request *request;
  1123. request = list_first_entry(&dev_priv->mm.request_list,
  1124. struct drm_i915_gem_request,
  1125. list);
  1126. ret = i915_wait_request(dev, request->seqno);
  1127. if (ret)
  1128. break;
  1129. /* if waiting caused an object to become inactive,
  1130. * then loop around and wait for it. Otherwise, we
  1131. * assume that waiting freed and unbound something,
  1132. * so there should now be some space in the GTT
  1133. */
  1134. if (!list_empty(&dev_priv->mm.inactive_list))
  1135. continue;
  1136. break;
  1137. }
  1138. /* If we didn't have anything on the request list but there
  1139. * are buffers awaiting a flush, emit one and try again.
  1140. * When we wait on it, those buffers waiting for that flush
  1141. * will get moved to inactive.
  1142. */
  1143. if (!list_empty(&dev_priv->mm.flushing_list)) {
  1144. obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
  1145. struct drm_i915_gem_object,
  1146. list);
  1147. obj = obj_priv->obj;
  1148. i915_gem_flush(dev,
  1149. obj->write_domain,
  1150. obj->write_domain);
  1151. i915_add_request(dev, obj->write_domain);
  1152. obj = NULL;
  1153. continue;
  1154. }
  1155. DRM_ERROR("inactive empty %d request empty %d "
  1156. "flushing empty %d\n",
  1157. list_empty(&dev_priv->mm.inactive_list),
  1158. list_empty(&dev_priv->mm.request_list),
  1159. list_empty(&dev_priv->mm.flushing_list));
  1160. /* If we didn't do any of the above, there's nothing to be done
  1161. * and we just can't fit it in.
  1162. */
  1163. return -ENOMEM;
  1164. }
  1165. return ret;
  1166. }
  1167. static int
  1168. i915_gem_evict_everything(struct drm_device *dev)
  1169. {
  1170. int ret;
  1171. for (;;) {
  1172. ret = i915_gem_evict_something(dev);
  1173. if (ret != 0)
  1174. break;
  1175. }
  1176. if (ret == -ENOMEM)
  1177. return 0;
  1178. return ret;
  1179. }
  1180. static int
  1181. i915_gem_object_get_page_list(struct drm_gem_object *obj)
  1182. {
  1183. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1184. int page_count, i;
  1185. struct address_space *mapping;
  1186. struct inode *inode;
  1187. struct page *page;
  1188. int ret;
  1189. if (obj_priv->page_list)
  1190. return 0;
  1191. /* Get the list of pages out of our struct file. They'll be pinned
  1192. * at this point until we release them.
  1193. */
  1194. page_count = obj->size / PAGE_SIZE;
  1195. BUG_ON(obj_priv->page_list != NULL);
  1196. obj_priv->page_list = drm_calloc(page_count, sizeof(struct page *),
  1197. DRM_MEM_DRIVER);
  1198. if (obj_priv->page_list == NULL) {
  1199. DRM_ERROR("Faled to allocate page list\n");
  1200. return -ENOMEM;
  1201. }
  1202. inode = obj->filp->f_path.dentry->d_inode;
  1203. mapping = inode->i_mapping;
  1204. for (i = 0; i < page_count; i++) {
  1205. page = read_mapping_page(mapping, i, NULL);
  1206. if (IS_ERR(page)) {
  1207. ret = PTR_ERR(page);
  1208. DRM_ERROR("read_mapping_page failed: %d\n", ret);
  1209. i915_gem_object_free_page_list(obj);
  1210. return ret;
  1211. }
  1212. obj_priv->page_list[i] = page;
  1213. }
  1214. return 0;
  1215. }
  1216. static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
  1217. {
  1218. struct drm_gem_object *obj = reg->obj;
  1219. struct drm_device *dev = obj->dev;
  1220. drm_i915_private_t *dev_priv = dev->dev_private;
  1221. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1222. int regnum = obj_priv->fence_reg;
  1223. uint64_t val;
  1224. val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
  1225. 0xfffff000) << 32;
  1226. val |= obj_priv->gtt_offset & 0xfffff000;
  1227. val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
  1228. if (obj_priv->tiling_mode == I915_TILING_Y)
  1229. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  1230. val |= I965_FENCE_REG_VALID;
  1231. I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
  1232. }
  1233. static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
  1234. {
  1235. struct drm_gem_object *obj = reg->obj;
  1236. struct drm_device *dev = obj->dev;
  1237. drm_i915_private_t *dev_priv = dev->dev_private;
  1238. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1239. int regnum = obj_priv->fence_reg;
  1240. int tile_width;
  1241. uint32_t val;
  1242. uint32_t pitch_val;
  1243. if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
  1244. (obj_priv->gtt_offset & (obj->size - 1))) {
  1245. WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
  1246. __func__, obj_priv->gtt_offset, obj->size);
  1247. return;
  1248. }
  1249. if (obj_priv->tiling_mode == I915_TILING_Y &&
  1250. HAS_128_BYTE_Y_TILING(dev))
  1251. tile_width = 128;
  1252. else
  1253. tile_width = 512;
  1254. /* Note: pitch better be a power of two tile widths */
  1255. pitch_val = obj_priv->stride / tile_width;
  1256. pitch_val = ffs(pitch_val) - 1;
  1257. val = obj_priv->gtt_offset;
  1258. if (obj_priv->tiling_mode == I915_TILING_Y)
  1259. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  1260. val |= I915_FENCE_SIZE_BITS(obj->size);
  1261. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  1262. val |= I830_FENCE_REG_VALID;
  1263. I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
  1264. }
  1265. static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
  1266. {
  1267. struct drm_gem_object *obj = reg->obj;
  1268. struct drm_device *dev = obj->dev;
  1269. drm_i915_private_t *dev_priv = dev->dev_private;
  1270. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1271. int regnum = obj_priv->fence_reg;
  1272. uint32_t val;
  1273. uint32_t pitch_val;
  1274. if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
  1275. (obj_priv->gtt_offset & (obj->size - 1))) {
  1276. WARN(1, "%s: object 0x%08x not 1M or size aligned\n",
  1277. __func__, obj_priv->gtt_offset);
  1278. return;
  1279. }
  1280. pitch_val = (obj_priv->stride / 128) - 1;
  1281. val = obj_priv->gtt_offset;
  1282. if (obj_priv->tiling_mode == I915_TILING_Y)
  1283. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  1284. val |= I830_FENCE_SIZE_BITS(obj->size);
  1285. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  1286. val |= I830_FENCE_REG_VALID;
  1287. I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
  1288. }
  1289. /**
  1290. * i915_gem_object_get_fence_reg - set up a fence reg for an object
  1291. * @obj: object to map through a fence reg
  1292. * @write: object is about to be written
  1293. *
  1294. * When mapping objects through the GTT, userspace wants to be able to write
  1295. * to them without having to worry about swizzling if the object is tiled.
  1296. *
  1297. * This function walks the fence regs looking for a free one for @obj,
  1298. * stealing one if it can't find any.
  1299. *
  1300. * It then sets up the reg based on the object's properties: address, pitch
  1301. * and tiling format.
  1302. */
  1303. static int
  1304. i915_gem_object_get_fence_reg(struct drm_gem_object *obj, bool write)
  1305. {
  1306. struct drm_device *dev = obj->dev;
  1307. struct drm_i915_private *dev_priv = dev->dev_private;
  1308. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1309. struct drm_i915_fence_reg *reg = NULL;
  1310. int i, ret;
  1311. switch (obj_priv->tiling_mode) {
  1312. case I915_TILING_NONE:
  1313. WARN(1, "allocating a fence for non-tiled object?\n");
  1314. break;
  1315. case I915_TILING_X:
  1316. if (!obj_priv->stride)
  1317. return -EINVAL;
  1318. WARN((obj_priv->stride & (512 - 1)),
  1319. "object 0x%08x is X tiled but has non-512B pitch\n",
  1320. obj_priv->gtt_offset);
  1321. break;
  1322. case I915_TILING_Y:
  1323. if (!obj_priv->stride)
  1324. return -EINVAL;
  1325. WARN((obj_priv->stride & (128 - 1)),
  1326. "object 0x%08x is Y tiled but has non-128B pitch\n",
  1327. obj_priv->gtt_offset);
  1328. break;
  1329. }
  1330. /* First try to find a free reg */
  1331. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  1332. reg = &dev_priv->fence_regs[i];
  1333. if (!reg->obj)
  1334. break;
  1335. }
  1336. /* None available, try to steal one or wait for a user to finish */
  1337. if (i == dev_priv->num_fence_regs) {
  1338. struct drm_i915_gem_object *old_obj_priv = NULL;
  1339. loff_t offset;
  1340. try_again:
  1341. /* Could try to use LRU here instead... */
  1342. for (i = dev_priv->fence_reg_start;
  1343. i < dev_priv->num_fence_regs; i++) {
  1344. reg = &dev_priv->fence_regs[i];
  1345. old_obj_priv = reg->obj->driver_private;
  1346. if (!old_obj_priv->pin_count)
  1347. break;
  1348. }
  1349. /*
  1350. * Now things get ugly... we have to wait for one of the
  1351. * objects to finish before trying again.
  1352. */
  1353. if (i == dev_priv->num_fence_regs) {
  1354. ret = i915_gem_object_set_to_gtt_domain(reg->obj, 0);
  1355. if (ret) {
  1356. WARN(ret != -ERESTARTSYS,
  1357. "switch to GTT domain failed: %d\n", ret);
  1358. return ret;
  1359. }
  1360. goto try_again;
  1361. }
  1362. /*
  1363. * Zap this virtual mapping so we can set up a fence again
  1364. * for this object next time we need it.
  1365. */
  1366. offset = ((loff_t) reg->obj->map_list.hash.key) << PAGE_SHIFT;
  1367. if (dev->dev_mapping)
  1368. unmap_mapping_range(dev->dev_mapping, offset,
  1369. reg->obj->size, 1);
  1370. old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
  1371. }
  1372. obj_priv->fence_reg = i;
  1373. reg->obj = obj;
  1374. if (IS_I965G(dev))
  1375. i965_write_fence_reg(reg);
  1376. else if (IS_I9XX(dev))
  1377. i915_write_fence_reg(reg);
  1378. else
  1379. i830_write_fence_reg(reg);
  1380. return 0;
  1381. }
  1382. /**
  1383. * i915_gem_clear_fence_reg - clear out fence register info
  1384. * @obj: object to clear
  1385. *
  1386. * Zeroes out the fence register itself and clears out the associated
  1387. * data structures in dev_priv and obj_priv.
  1388. */
  1389. static void
  1390. i915_gem_clear_fence_reg(struct drm_gem_object *obj)
  1391. {
  1392. struct drm_device *dev = obj->dev;
  1393. drm_i915_private_t *dev_priv = dev->dev_private;
  1394. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1395. if (IS_I965G(dev))
  1396. I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
  1397. else
  1398. I915_WRITE(FENCE_REG_830_0 + (obj_priv->fence_reg * 4), 0);
  1399. dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
  1400. obj_priv->fence_reg = I915_FENCE_REG_NONE;
  1401. }
  1402. /**
  1403. * Finds free space in the GTT aperture and binds the object there.
  1404. */
  1405. static int
  1406. i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
  1407. {
  1408. struct drm_device *dev = obj->dev;
  1409. drm_i915_private_t *dev_priv = dev->dev_private;
  1410. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1411. struct drm_mm_node *free_space;
  1412. int page_count, ret;
  1413. if (dev_priv->mm.suspended)
  1414. return -EBUSY;
  1415. if (alignment == 0)
  1416. alignment = i915_gem_get_gtt_alignment(obj);
  1417. if (alignment & (PAGE_SIZE - 1)) {
  1418. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  1419. return -EINVAL;
  1420. }
  1421. search_free:
  1422. free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
  1423. obj->size, alignment, 0);
  1424. if (free_space != NULL) {
  1425. obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
  1426. alignment);
  1427. if (obj_priv->gtt_space != NULL) {
  1428. obj_priv->gtt_space->private = obj;
  1429. obj_priv->gtt_offset = obj_priv->gtt_space->start;
  1430. }
  1431. }
  1432. if (obj_priv->gtt_space == NULL) {
  1433. /* If the gtt is empty and we're still having trouble
  1434. * fitting our object in, we're out of memory.
  1435. */
  1436. #if WATCH_LRU
  1437. DRM_INFO("%s: GTT full, evicting something\n", __func__);
  1438. #endif
  1439. if (list_empty(&dev_priv->mm.inactive_list) &&
  1440. list_empty(&dev_priv->mm.flushing_list) &&
  1441. list_empty(&dev_priv->mm.active_list)) {
  1442. DRM_ERROR("GTT full, but LRU list empty\n");
  1443. return -ENOMEM;
  1444. }
  1445. ret = i915_gem_evict_something(dev);
  1446. if (ret != 0) {
  1447. if (ret != -ERESTARTSYS)
  1448. DRM_ERROR("Failed to evict a buffer %d\n", ret);
  1449. return ret;
  1450. }
  1451. goto search_free;
  1452. }
  1453. #if WATCH_BUF
  1454. DRM_INFO("Binding object of size %d at 0x%08x\n",
  1455. obj->size, obj_priv->gtt_offset);
  1456. #endif
  1457. ret = i915_gem_object_get_page_list(obj);
  1458. if (ret) {
  1459. drm_mm_put_block(obj_priv->gtt_space);
  1460. obj_priv->gtt_space = NULL;
  1461. return ret;
  1462. }
  1463. page_count = obj->size / PAGE_SIZE;
  1464. /* Create an AGP memory structure pointing at our pages, and bind it
  1465. * into the GTT.
  1466. */
  1467. obj_priv->agp_mem = drm_agp_bind_pages(dev,
  1468. obj_priv->page_list,
  1469. page_count,
  1470. obj_priv->gtt_offset,
  1471. obj_priv->agp_type);
  1472. if (obj_priv->agp_mem == NULL) {
  1473. i915_gem_object_free_page_list(obj);
  1474. drm_mm_put_block(obj_priv->gtt_space);
  1475. obj_priv->gtt_space = NULL;
  1476. return -ENOMEM;
  1477. }
  1478. atomic_inc(&dev->gtt_count);
  1479. atomic_add(obj->size, &dev->gtt_memory);
  1480. /* Assert that the object is not currently in any GPU domain. As it
  1481. * wasn't in the GTT, there shouldn't be any way it could have been in
  1482. * a GPU cache
  1483. */
  1484. BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  1485. BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  1486. return 0;
  1487. }
  1488. void
  1489. i915_gem_clflush_object(struct drm_gem_object *obj)
  1490. {
  1491. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1492. /* If we don't have a page list set up, then we're not pinned
  1493. * to GPU, and we can ignore the cache flush because it'll happen
  1494. * again at bind time.
  1495. */
  1496. if (obj_priv->page_list == NULL)
  1497. return;
  1498. drm_clflush_pages(obj_priv->page_list, obj->size / PAGE_SIZE);
  1499. }
  1500. /** Flushes any GPU write domain for the object if it's dirty. */
  1501. static void
  1502. i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
  1503. {
  1504. struct drm_device *dev = obj->dev;
  1505. uint32_t seqno;
  1506. if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
  1507. return;
  1508. /* Queue the GPU write cache flushing we need. */
  1509. i915_gem_flush(dev, 0, obj->write_domain);
  1510. seqno = i915_add_request(dev, obj->write_domain);
  1511. obj->write_domain = 0;
  1512. i915_gem_object_move_to_active(obj, seqno);
  1513. }
  1514. /** Flushes the GTT write domain for the object if it's dirty. */
  1515. static void
  1516. i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
  1517. {
  1518. if (obj->write_domain != I915_GEM_DOMAIN_GTT)
  1519. return;
  1520. /* No actual flushing is required for the GTT write domain. Writes
  1521. * to it immediately go to main memory as far as we know, so there's
  1522. * no chipset flush. It also doesn't land in render cache.
  1523. */
  1524. obj->write_domain = 0;
  1525. }
  1526. /** Flushes the CPU write domain for the object if it's dirty. */
  1527. static void
  1528. i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
  1529. {
  1530. struct drm_device *dev = obj->dev;
  1531. if (obj->write_domain != I915_GEM_DOMAIN_CPU)
  1532. return;
  1533. i915_gem_clflush_object(obj);
  1534. drm_agp_chipset_flush(dev);
  1535. obj->write_domain = 0;
  1536. }
  1537. /**
  1538. * Moves a single object to the GTT read, and possibly write domain.
  1539. *
  1540. * This function returns when the move is complete, including waiting on
  1541. * flushes to occur.
  1542. */
  1543. int
  1544. i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
  1545. {
  1546. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1547. int ret;
  1548. /* Not valid to be called on unbound objects. */
  1549. if (obj_priv->gtt_space == NULL)
  1550. return -EINVAL;
  1551. i915_gem_object_flush_gpu_write_domain(obj);
  1552. /* Wait on any GPU rendering and flushing to occur. */
  1553. ret = i915_gem_object_wait_rendering(obj);
  1554. if (ret != 0)
  1555. return ret;
  1556. /* If we're writing through the GTT domain, then CPU and GPU caches
  1557. * will need to be invalidated at next use.
  1558. */
  1559. if (write)
  1560. obj->read_domains &= I915_GEM_DOMAIN_GTT;
  1561. i915_gem_object_flush_cpu_write_domain(obj);
  1562. /* It should now be out of any other write domains, and we can update
  1563. * the domain values for our changes.
  1564. */
  1565. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  1566. obj->read_domains |= I915_GEM_DOMAIN_GTT;
  1567. if (write) {
  1568. obj->write_domain = I915_GEM_DOMAIN_GTT;
  1569. obj_priv->dirty = 1;
  1570. }
  1571. return 0;
  1572. }
  1573. /**
  1574. * Moves a single object to the CPU read, and possibly write domain.
  1575. *
  1576. * This function returns when the move is complete, including waiting on
  1577. * flushes to occur.
  1578. */
  1579. static int
  1580. i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
  1581. {
  1582. struct drm_device *dev = obj->dev;
  1583. int ret;
  1584. i915_gem_object_flush_gpu_write_domain(obj);
  1585. /* Wait on any GPU rendering and flushing to occur. */
  1586. ret = i915_gem_object_wait_rendering(obj);
  1587. if (ret != 0)
  1588. return ret;
  1589. i915_gem_object_flush_gtt_write_domain(obj);
  1590. /* If we have a partially-valid cache of the object in the CPU,
  1591. * finish invalidating it and free the per-page flags.
  1592. */
  1593. i915_gem_object_set_to_full_cpu_read_domain(obj);
  1594. /* Flush the CPU cache if it's still invalid. */
  1595. if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  1596. i915_gem_clflush_object(obj);
  1597. drm_agp_chipset_flush(dev);
  1598. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  1599. }
  1600. /* It should now be out of any other write domains, and we can update
  1601. * the domain values for our changes.
  1602. */
  1603. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  1604. /* If we're writing through the CPU, then the GPU read domains will
  1605. * need to be invalidated at next use.
  1606. */
  1607. if (write) {
  1608. obj->read_domains &= I915_GEM_DOMAIN_CPU;
  1609. obj->write_domain = I915_GEM_DOMAIN_CPU;
  1610. }
  1611. return 0;
  1612. }
  1613. /*
  1614. * Set the next domain for the specified object. This
  1615. * may not actually perform the necessary flushing/invaliding though,
  1616. * as that may want to be batched with other set_domain operations
  1617. *
  1618. * This is (we hope) the only really tricky part of gem. The goal
  1619. * is fairly simple -- track which caches hold bits of the object
  1620. * and make sure they remain coherent. A few concrete examples may
  1621. * help to explain how it works. For shorthand, we use the notation
  1622. * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
  1623. * a pair of read and write domain masks.
  1624. *
  1625. * Case 1: the batch buffer
  1626. *
  1627. * 1. Allocated
  1628. * 2. Written by CPU
  1629. * 3. Mapped to GTT
  1630. * 4. Read by GPU
  1631. * 5. Unmapped from GTT
  1632. * 6. Freed
  1633. *
  1634. * Let's take these a step at a time
  1635. *
  1636. * 1. Allocated
  1637. * Pages allocated from the kernel may still have
  1638. * cache contents, so we set them to (CPU, CPU) always.
  1639. * 2. Written by CPU (using pwrite)
  1640. * The pwrite function calls set_domain (CPU, CPU) and
  1641. * this function does nothing (as nothing changes)
  1642. * 3. Mapped by GTT
  1643. * This function asserts that the object is not
  1644. * currently in any GPU-based read or write domains
  1645. * 4. Read by GPU
  1646. * i915_gem_execbuffer calls set_domain (COMMAND, 0).
  1647. * As write_domain is zero, this function adds in the
  1648. * current read domains (CPU+COMMAND, 0).
  1649. * flush_domains is set to CPU.
  1650. * invalidate_domains is set to COMMAND
  1651. * clflush is run to get data out of the CPU caches
  1652. * then i915_dev_set_domain calls i915_gem_flush to
  1653. * emit an MI_FLUSH and drm_agp_chipset_flush
  1654. * 5. Unmapped from GTT
  1655. * i915_gem_object_unbind calls set_domain (CPU, CPU)
  1656. * flush_domains and invalidate_domains end up both zero
  1657. * so no flushing/invalidating happens
  1658. * 6. Freed
  1659. * yay, done
  1660. *
  1661. * Case 2: The shared render buffer
  1662. *
  1663. * 1. Allocated
  1664. * 2. Mapped to GTT
  1665. * 3. Read/written by GPU
  1666. * 4. set_domain to (CPU,CPU)
  1667. * 5. Read/written by CPU
  1668. * 6. Read/written by GPU
  1669. *
  1670. * 1. Allocated
  1671. * Same as last example, (CPU, CPU)
  1672. * 2. Mapped to GTT
  1673. * Nothing changes (assertions find that it is not in the GPU)
  1674. * 3. Read/written by GPU
  1675. * execbuffer calls set_domain (RENDER, RENDER)
  1676. * flush_domains gets CPU
  1677. * invalidate_domains gets GPU
  1678. * clflush (obj)
  1679. * MI_FLUSH and drm_agp_chipset_flush
  1680. * 4. set_domain (CPU, CPU)
  1681. * flush_domains gets GPU
  1682. * invalidate_domains gets CPU
  1683. * wait_rendering (obj) to make sure all drawing is complete.
  1684. * This will include an MI_FLUSH to get the data from GPU
  1685. * to memory
  1686. * clflush (obj) to invalidate the CPU cache
  1687. * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
  1688. * 5. Read/written by CPU
  1689. * cache lines are loaded and dirtied
  1690. * 6. Read written by GPU
  1691. * Same as last GPU access
  1692. *
  1693. * Case 3: The constant buffer
  1694. *
  1695. * 1. Allocated
  1696. * 2. Written by CPU
  1697. * 3. Read by GPU
  1698. * 4. Updated (written) by CPU again
  1699. * 5. Read by GPU
  1700. *
  1701. * 1. Allocated
  1702. * (CPU, CPU)
  1703. * 2. Written by CPU
  1704. * (CPU, CPU)
  1705. * 3. Read by GPU
  1706. * (CPU+RENDER, 0)
  1707. * flush_domains = CPU
  1708. * invalidate_domains = RENDER
  1709. * clflush (obj)
  1710. * MI_FLUSH
  1711. * drm_agp_chipset_flush
  1712. * 4. Updated (written) by CPU again
  1713. * (CPU, CPU)
  1714. * flush_domains = 0 (no previous write domain)
  1715. * invalidate_domains = 0 (no new read domains)
  1716. * 5. Read by GPU
  1717. * (CPU+RENDER, 0)
  1718. * flush_domains = CPU
  1719. * invalidate_domains = RENDER
  1720. * clflush (obj)
  1721. * MI_FLUSH
  1722. * drm_agp_chipset_flush
  1723. */
  1724. static void
  1725. i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
  1726. uint32_t read_domains,
  1727. uint32_t write_domain)
  1728. {
  1729. struct drm_device *dev = obj->dev;
  1730. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1731. uint32_t invalidate_domains = 0;
  1732. uint32_t flush_domains = 0;
  1733. BUG_ON(read_domains & I915_GEM_DOMAIN_CPU);
  1734. BUG_ON(write_domain == I915_GEM_DOMAIN_CPU);
  1735. #if WATCH_BUF
  1736. DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
  1737. __func__, obj,
  1738. obj->read_domains, read_domains,
  1739. obj->write_domain, write_domain);
  1740. #endif
  1741. /*
  1742. * If the object isn't moving to a new write domain,
  1743. * let the object stay in multiple read domains
  1744. */
  1745. if (write_domain == 0)
  1746. read_domains |= obj->read_domains;
  1747. else
  1748. obj_priv->dirty = 1;
  1749. /*
  1750. * Flush the current write domain if
  1751. * the new read domains don't match. Invalidate
  1752. * any read domains which differ from the old
  1753. * write domain
  1754. */
  1755. if (obj->write_domain && obj->write_domain != read_domains) {
  1756. flush_domains |= obj->write_domain;
  1757. invalidate_domains |= read_domains & ~obj->write_domain;
  1758. }
  1759. /*
  1760. * Invalidate any read caches which may have
  1761. * stale data. That is, any new read domains.
  1762. */
  1763. invalidate_domains |= read_domains & ~obj->read_domains;
  1764. if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
  1765. #if WATCH_BUF
  1766. DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
  1767. __func__, flush_domains, invalidate_domains);
  1768. #endif
  1769. i915_gem_clflush_object(obj);
  1770. }
  1771. if ((write_domain | flush_domains) != 0)
  1772. obj->write_domain = write_domain;
  1773. obj->read_domains = read_domains;
  1774. dev->invalidate_domains |= invalidate_domains;
  1775. dev->flush_domains |= flush_domains;
  1776. #if WATCH_BUF
  1777. DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
  1778. __func__,
  1779. obj->read_domains, obj->write_domain,
  1780. dev->invalidate_domains, dev->flush_domains);
  1781. #endif
  1782. }
  1783. /**
  1784. * Moves the object from a partially CPU read to a full one.
  1785. *
  1786. * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
  1787. * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
  1788. */
  1789. static void
  1790. i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
  1791. {
  1792. struct drm_device *dev = obj->dev;
  1793. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1794. if (!obj_priv->page_cpu_valid)
  1795. return;
  1796. /* If we're partially in the CPU read domain, finish moving it in.
  1797. */
  1798. if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
  1799. int i;
  1800. for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
  1801. if (obj_priv->page_cpu_valid[i])
  1802. continue;
  1803. drm_clflush_pages(obj_priv->page_list + i, 1);
  1804. }
  1805. drm_agp_chipset_flush(dev);
  1806. }
  1807. /* Free the page_cpu_valid mappings which are now stale, whether
  1808. * or not we've got I915_GEM_DOMAIN_CPU.
  1809. */
  1810. drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
  1811. DRM_MEM_DRIVER);
  1812. obj_priv->page_cpu_valid = NULL;
  1813. }
  1814. /**
  1815. * Set the CPU read domain on a range of the object.
  1816. *
  1817. * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
  1818. * not entirely valid. The page_cpu_valid member of the object flags which
  1819. * pages have been flushed, and will be respected by
  1820. * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
  1821. * of the whole object.
  1822. *
  1823. * This function returns when the move is complete, including waiting on
  1824. * flushes to occur.
  1825. */
  1826. static int
  1827. i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
  1828. uint64_t offset, uint64_t size)
  1829. {
  1830. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1831. int i, ret;
  1832. if (offset == 0 && size == obj->size)
  1833. return i915_gem_object_set_to_cpu_domain(obj, 0);
  1834. i915_gem_object_flush_gpu_write_domain(obj);
  1835. /* Wait on any GPU rendering and flushing to occur. */
  1836. ret = i915_gem_object_wait_rendering(obj);
  1837. if (ret != 0)
  1838. return ret;
  1839. i915_gem_object_flush_gtt_write_domain(obj);
  1840. /* If we're already fully in the CPU read domain, we're done. */
  1841. if (obj_priv->page_cpu_valid == NULL &&
  1842. (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
  1843. return 0;
  1844. /* Otherwise, create/clear the per-page CPU read domain flag if we're
  1845. * newly adding I915_GEM_DOMAIN_CPU
  1846. */
  1847. if (obj_priv->page_cpu_valid == NULL) {
  1848. obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
  1849. DRM_MEM_DRIVER);
  1850. if (obj_priv->page_cpu_valid == NULL)
  1851. return -ENOMEM;
  1852. } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
  1853. memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
  1854. /* Flush the cache on any pages that are still invalid from the CPU's
  1855. * perspective.
  1856. */
  1857. for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
  1858. i++) {
  1859. if (obj_priv->page_cpu_valid[i])
  1860. continue;
  1861. drm_clflush_pages(obj_priv->page_list + i, 1);
  1862. obj_priv->page_cpu_valid[i] = 1;
  1863. }
  1864. /* It should now be out of any other write domains, and we can update
  1865. * the domain values for our changes.
  1866. */
  1867. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  1868. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  1869. return 0;
  1870. }
  1871. /**
  1872. * Pin an object to the GTT and evaluate the relocations landing in it.
  1873. */
  1874. static int
  1875. i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
  1876. struct drm_file *file_priv,
  1877. struct drm_i915_gem_exec_object *entry)
  1878. {
  1879. struct drm_device *dev = obj->dev;
  1880. drm_i915_private_t *dev_priv = dev->dev_private;
  1881. struct drm_i915_gem_relocation_entry reloc;
  1882. struct drm_i915_gem_relocation_entry __user *relocs;
  1883. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1884. int i, ret;
  1885. void __iomem *reloc_page;
  1886. /* Choose the GTT offset for our buffer and put it there. */
  1887. ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
  1888. if (ret)
  1889. return ret;
  1890. entry->offset = obj_priv->gtt_offset;
  1891. relocs = (struct drm_i915_gem_relocation_entry __user *)
  1892. (uintptr_t) entry->relocs_ptr;
  1893. /* Apply the relocations, using the GTT aperture to avoid cache
  1894. * flushing requirements.
  1895. */
  1896. for (i = 0; i < entry->relocation_count; i++) {
  1897. struct drm_gem_object *target_obj;
  1898. struct drm_i915_gem_object *target_obj_priv;
  1899. uint32_t reloc_val, reloc_offset;
  1900. uint32_t __iomem *reloc_entry;
  1901. ret = copy_from_user(&reloc, relocs + i, sizeof(reloc));
  1902. if (ret != 0) {
  1903. i915_gem_object_unpin(obj);
  1904. return ret;
  1905. }
  1906. target_obj = drm_gem_object_lookup(obj->dev, file_priv,
  1907. reloc.target_handle);
  1908. if (target_obj == NULL) {
  1909. i915_gem_object_unpin(obj);
  1910. return -EBADF;
  1911. }
  1912. target_obj_priv = target_obj->driver_private;
  1913. /* The target buffer should have appeared before us in the
  1914. * exec_object list, so it should have a GTT space bound by now.
  1915. */
  1916. if (target_obj_priv->gtt_space == NULL) {
  1917. DRM_ERROR("No GTT space found for object %d\n",
  1918. reloc.target_handle);
  1919. drm_gem_object_unreference(target_obj);
  1920. i915_gem_object_unpin(obj);
  1921. return -EINVAL;
  1922. }
  1923. if (reloc.offset > obj->size - 4) {
  1924. DRM_ERROR("Relocation beyond object bounds: "
  1925. "obj %p target %d offset %d size %d.\n",
  1926. obj, reloc.target_handle,
  1927. (int) reloc.offset, (int) obj->size);
  1928. drm_gem_object_unreference(target_obj);
  1929. i915_gem_object_unpin(obj);
  1930. return -EINVAL;
  1931. }
  1932. if (reloc.offset & 3) {
  1933. DRM_ERROR("Relocation not 4-byte aligned: "
  1934. "obj %p target %d offset %d.\n",
  1935. obj, reloc.target_handle,
  1936. (int) reloc.offset);
  1937. drm_gem_object_unreference(target_obj);
  1938. i915_gem_object_unpin(obj);
  1939. return -EINVAL;
  1940. }
  1941. if (reloc.write_domain & I915_GEM_DOMAIN_CPU ||
  1942. reloc.read_domains & I915_GEM_DOMAIN_CPU) {
  1943. DRM_ERROR("reloc with read/write CPU domains: "
  1944. "obj %p target %d offset %d "
  1945. "read %08x write %08x",
  1946. obj, reloc.target_handle,
  1947. (int) reloc.offset,
  1948. reloc.read_domains,
  1949. reloc.write_domain);
  1950. return -EINVAL;
  1951. }
  1952. if (reloc.write_domain && target_obj->pending_write_domain &&
  1953. reloc.write_domain != target_obj->pending_write_domain) {
  1954. DRM_ERROR("Write domain conflict: "
  1955. "obj %p target %d offset %d "
  1956. "new %08x old %08x\n",
  1957. obj, reloc.target_handle,
  1958. (int) reloc.offset,
  1959. reloc.write_domain,
  1960. target_obj->pending_write_domain);
  1961. drm_gem_object_unreference(target_obj);
  1962. i915_gem_object_unpin(obj);
  1963. return -EINVAL;
  1964. }
  1965. #if WATCH_RELOC
  1966. DRM_INFO("%s: obj %p offset %08x target %d "
  1967. "read %08x write %08x gtt %08x "
  1968. "presumed %08x delta %08x\n",
  1969. __func__,
  1970. obj,
  1971. (int) reloc.offset,
  1972. (int) reloc.target_handle,
  1973. (int) reloc.read_domains,
  1974. (int) reloc.write_domain,
  1975. (int) target_obj_priv->gtt_offset,
  1976. (int) reloc.presumed_offset,
  1977. reloc.delta);
  1978. #endif
  1979. target_obj->pending_read_domains |= reloc.read_domains;
  1980. target_obj->pending_write_domain |= reloc.write_domain;
  1981. /* If the relocation already has the right value in it, no
  1982. * more work needs to be done.
  1983. */
  1984. if (target_obj_priv->gtt_offset == reloc.presumed_offset) {
  1985. drm_gem_object_unreference(target_obj);
  1986. continue;
  1987. }
  1988. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  1989. if (ret != 0) {
  1990. drm_gem_object_unreference(target_obj);
  1991. i915_gem_object_unpin(obj);
  1992. return -EINVAL;
  1993. }
  1994. /* Map the page containing the relocation we're going to
  1995. * perform.
  1996. */
  1997. reloc_offset = obj_priv->gtt_offset + reloc.offset;
  1998. reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
  1999. (reloc_offset &
  2000. ~(PAGE_SIZE - 1)));
  2001. reloc_entry = (uint32_t __iomem *)(reloc_page +
  2002. (reloc_offset & (PAGE_SIZE - 1)));
  2003. reloc_val = target_obj_priv->gtt_offset + reloc.delta;
  2004. #if WATCH_BUF
  2005. DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
  2006. obj, (unsigned int) reloc.offset,
  2007. readl(reloc_entry), reloc_val);
  2008. #endif
  2009. writel(reloc_val, reloc_entry);
  2010. io_mapping_unmap_atomic(reloc_page);
  2011. /* Write the updated presumed offset for this entry back out
  2012. * to the user.
  2013. */
  2014. reloc.presumed_offset = target_obj_priv->gtt_offset;
  2015. ret = copy_to_user(relocs + i, &reloc, sizeof(reloc));
  2016. if (ret != 0) {
  2017. drm_gem_object_unreference(target_obj);
  2018. i915_gem_object_unpin(obj);
  2019. return ret;
  2020. }
  2021. drm_gem_object_unreference(target_obj);
  2022. }
  2023. #if WATCH_BUF
  2024. if (0)
  2025. i915_gem_dump_object(obj, 128, __func__, ~0);
  2026. #endif
  2027. return 0;
  2028. }
  2029. /** Dispatch a batchbuffer to the ring
  2030. */
  2031. static int
  2032. i915_dispatch_gem_execbuffer(struct drm_device *dev,
  2033. struct drm_i915_gem_execbuffer *exec,
  2034. uint64_t exec_offset)
  2035. {
  2036. drm_i915_private_t *dev_priv = dev->dev_private;
  2037. struct drm_clip_rect __user *boxes = (struct drm_clip_rect __user *)
  2038. (uintptr_t) exec->cliprects_ptr;
  2039. int nbox = exec->num_cliprects;
  2040. int i = 0, count;
  2041. uint32_t exec_start, exec_len;
  2042. RING_LOCALS;
  2043. exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
  2044. exec_len = (uint32_t) exec->batch_len;
  2045. if ((exec_start | exec_len) & 0x7) {
  2046. DRM_ERROR("alignment\n");
  2047. return -EINVAL;
  2048. }
  2049. if (!exec_start)
  2050. return -EINVAL;
  2051. count = nbox ? nbox : 1;
  2052. for (i = 0; i < count; i++) {
  2053. if (i < nbox) {
  2054. int ret = i915_emit_box(dev, boxes, i,
  2055. exec->DR1, exec->DR4);
  2056. if (ret)
  2057. return ret;
  2058. }
  2059. if (IS_I830(dev) || IS_845G(dev)) {
  2060. BEGIN_LP_RING(4);
  2061. OUT_RING(MI_BATCH_BUFFER);
  2062. OUT_RING(exec_start | MI_BATCH_NON_SECURE);
  2063. OUT_RING(exec_start + exec_len - 4);
  2064. OUT_RING(0);
  2065. ADVANCE_LP_RING();
  2066. } else {
  2067. BEGIN_LP_RING(2);
  2068. if (IS_I965G(dev)) {
  2069. OUT_RING(MI_BATCH_BUFFER_START |
  2070. (2 << 6) |
  2071. MI_BATCH_NON_SECURE_I965);
  2072. OUT_RING(exec_start);
  2073. } else {
  2074. OUT_RING(MI_BATCH_BUFFER_START |
  2075. (2 << 6));
  2076. OUT_RING(exec_start | MI_BATCH_NON_SECURE);
  2077. }
  2078. ADVANCE_LP_RING();
  2079. }
  2080. }
  2081. /* XXX breadcrumb */
  2082. return 0;
  2083. }
  2084. /* Throttle our rendering by waiting until the ring has completed our requests
  2085. * emitted over 20 msec ago.
  2086. *
  2087. * This should get us reasonable parallelism between CPU and GPU but also
  2088. * relatively low latency when blocking on a particular request to finish.
  2089. */
  2090. static int
  2091. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
  2092. {
  2093. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  2094. int ret = 0;
  2095. uint32_t seqno;
  2096. mutex_lock(&dev->struct_mutex);
  2097. seqno = i915_file_priv->mm.last_gem_throttle_seqno;
  2098. i915_file_priv->mm.last_gem_throttle_seqno =
  2099. i915_file_priv->mm.last_gem_seqno;
  2100. if (seqno)
  2101. ret = i915_wait_request(dev, seqno);
  2102. mutex_unlock(&dev->struct_mutex);
  2103. return ret;
  2104. }
  2105. int
  2106. i915_gem_execbuffer(struct drm_device *dev, void *data,
  2107. struct drm_file *file_priv)
  2108. {
  2109. drm_i915_private_t *dev_priv = dev->dev_private;
  2110. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  2111. struct drm_i915_gem_execbuffer *args = data;
  2112. struct drm_i915_gem_exec_object *exec_list = NULL;
  2113. struct drm_gem_object **object_list = NULL;
  2114. struct drm_gem_object *batch_obj;
  2115. int ret, i, pinned = 0;
  2116. uint64_t exec_offset;
  2117. uint32_t seqno, flush_domains;
  2118. int pin_tries;
  2119. #if WATCH_EXEC
  2120. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  2121. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  2122. #endif
  2123. if (args->buffer_count < 1) {
  2124. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  2125. return -EINVAL;
  2126. }
  2127. /* Copy in the exec list from userland */
  2128. exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count,
  2129. DRM_MEM_DRIVER);
  2130. object_list = drm_calloc(sizeof(*object_list), args->buffer_count,
  2131. DRM_MEM_DRIVER);
  2132. if (exec_list == NULL || object_list == NULL) {
  2133. DRM_ERROR("Failed to allocate exec or object list "
  2134. "for %d buffers\n",
  2135. args->buffer_count);
  2136. ret = -ENOMEM;
  2137. goto pre_mutex_err;
  2138. }
  2139. ret = copy_from_user(exec_list,
  2140. (struct drm_i915_relocation_entry __user *)
  2141. (uintptr_t) args->buffers_ptr,
  2142. sizeof(*exec_list) * args->buffer_count);
  2143. if (ret != 0) {
  2144. DRM_ERROR("copy %d exec entries failed %d\n",
  2145. args->buffer_count, ret);
  2146. goto pre_mutex_err;
  2147. }
  2148. mutex_lock(&dev->struct_mutex);
  2149. i915_verify_inactive(dev, __FILE__, __LINE__);
  2150. if (dev_priv->mm.wedged) {
  2151. DRM_ERROR("Execbuf while wedged\n");
  2152. mutex_unlock(&dev->struct_mutex);
  2153. return -EIO;
  2154. }
  2155. if (dev_priv->mm.suspended) {
  2156. DRM_ERROR("Execbuf while VT-switched.\n");
  2157. mutex_unlock(&dev->struct_mutex);
  2158. return -EBUSY;
  2159. }
  2160. /* Look up object handles */
  2161. for (i = 0; i < args->buffer_count; i++) {
  2162. object_list[i] = drm_gem_object_lookup(dev, file_priv,
  2163. exec_list[i].handle);
  2164. if (object_list[i] == NULL) {
  2165. DRM_ERROR("Invalid object handle %d at index %d\n",
  2166. exec_list[i].handle, i);
  2167. ret = -EBADF;
  2168. goto err;
  2169. }
  2170. }
  2171. /* Pin and relocate */
  2172. for (pin_tries = 0; ; pin_tries++) {
  2173. ret = 0;
  2174. for (i = 0; i < args->buffer_count; i++) {
  2175. object_list[i]->pending_read_domains = 0;
  2176. object_list[i]->pending_write_domain = 0;
  2177. ret = i915_gem_object_pin_and_relocate(object_list[i],
  2178. file_priv,
  2179. &exec_list[i]);
  2180. if (ret)
  2181. break;
  2182. pinned = i + 1;
  2183. }
  2184. /* success */
  2185. if (ret == 0)
  2186. break;
  2187. /* error other than GTT full, or we've already tried again */
  2188. if (ret != -ENOMEM || pin_tries >= 1) {
  2189. if (ret != -ERESTARTSYS)
  2190. DRM_ERROR("Failed to pin buffers %d\n", ret);
  2191. goto err;
  2192. }
  2193. /* unpin all of our buffers */
  2194. for (i = 0; i < pinned; i++)
  2195. i915_gem_object_unpin(object_list[i]);
  2196. pinned = 0;
  2197. /* evict everyone we can from the aperture */
  2198. ret = i915_gem_evict_everything(dev);
  2199. if (ret)
  2200. goto err;
  2201. }
  2202. /* Set the pending read domains for the batch buffer to COMMAND */
  2203. batch_obj = object_list[args->buffer_count-1];
  2204. batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND;
  2205. batch_obj->pending_write_domain = 0;
  2206. i915_verify_inactive(dev, __FILE__, __LINE__);
  2207. /* Zero the global flush/invalidate flags. These
  2208. * will be modified as new domains are computed
  2209. * for each object
  2210. */
  2211. dev->invalidate_domains = 0;
  2212. dev->flush_domains = 0;
  2213. for (i = 0; i < args->buffer_count; i++) {
  2214. struct drm_gem_object *obj = object_list[i];
  2215. /* Compute new gpu domains and update invalidate/flush */
  2216. i915_gem_object_set_to_gpu_domain(obj,
  2217. obj->pending_read_domains,
  2218. obj->pending_write_domain);
  2219. }
  2220. i915_verify_inactive(dev, __FILE__, __LINE__);
  2221. if (dev->invalidate_domains | dev->flush_domains) {
  2222. #if WATCH_EXEC
  2223. DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
  2224. __func__,
  2225. dev->invalidate_domains,
  2226. dev->flush_domains);
  2227. #endif
  2228. i915_gem_flush(dev,
  2229. dev->invalidate_domains,
  2230. dev->flush_domains);
  2231. if (dev->flush_domains)
  2232. (void)i915_add_request(dev, dev->flush_domains);
  2233. }
  2234. i915_verify_inactive(dev, __FILE__, __LINE__);
  2235. #if WATCH_COHERENCY
  2236. for (i = 0; i < args->buffer_count; i++) {
  2237. i915_gem_object_check_coherency(object_list[i],
  2238. exec_list[i].handle);
  2239. }
  2240. #endif
  2241. exec_offset = exec_list[args->buffer_count - 1].offset;
  2242. #if WATCH_EXEC
  2243. i915_gem_dump_object(object_list[args->buffer_count - 1],
  2244. args->batch_len,
  2245. __func__,
  2246. ~0);
  2247. #endif
  2248. /* Exec the batchbuffer */
  2249. ret = i915_dispatch_gem_execbuffer(dev, args, exec_offset);
  2250. if (ret) {
  2251. DRM_ERROR("dispatch failed %d\n", ret);
  2252. goto err;
  2253. }
  2254. /*
  2255. * Ensure that the commands in the batch buffer are
  2256. * finished before the interrupt fires
  2257. */
  2258. flush_domains = i915_retire_commands(dev);
  2259. i915_verify_inactive(dev, __FILE__, __LINE__);
  2260. /*
  2261. * Get a seqno representing the execution of the current buffer,
  2262. * which we can wait on. We would like to mitigate these interrupts,
  2263. * likely by only creating seqnos occasionally (so that we have
  2264. * *some* interrupts representing completion of buffers that we can
  2265. * wait on when trying to clear up gtt space).
  2266. */
  2267. seqno = i915_add_request(dev, flush_domains);
  2268. BUG_ON(seqno == 0);
  2269. i915_file_priv->mm.last_gem_seqno = seqno;
  2270. for (i = 0; i < args->buffer_count; i++) {
  2271. struct drm_gem_object *obj = object_list[i];
  2272. i915_gem_object_move_to_active(obj, seqno);
  2273. #if WATCH_LRU
  2274. DRM_INFO("%s: move to exec list %p\n", __func__, obj);
  2275. #endif
  2276. }
  2277. #if WATCH_LRU
  2278. i915_dump_lru(dev, __func__);
  2279. #endif
  2280. i915_verify_inactive(dev, __FILE__, __LINE__);
  2281. /* Copy the new buffer offsets back to the user's exec list. */
  2282. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  2283. (uintptr_t) args->buffers_ptr,
  2284. exec_list,
  2285. sizeof(*exec_list) * args->buffer_count);
  2286. if (ret)
  2287. DRM_ERROR("failed to copy %d exec entries "
  2288. "back to user (%d)\n",
  2289. args->buffer_count, ret);
  2290. err:
  2291. for (i = 0; i < pinned; i++)
  2292. i915_gem_object_unpin(object_list[i]);
  2293. for (i = 0; i < args->buffer_count; i++)
  2294. drm_gem_object_unreference(object_list[i]);
  2295. mutex_unlock(&dev->struct_mutex);
  2296. pre_mutex_err:
  2297. drm_free(object_list, sizeof(*object_list) * args->buffer_count,
  2298. DRM_MEM_DRIVER);
  2299. drm_free(exec_list, sizeof(*exec_list) * args->buffer_count,
  2300. DRM_MEM_DRIVER);
  2301. return ret;
  2302. }
  2303. int
  2304. i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
  2305. {
  2306. struct drm_device *dev = obj->dev;
  2307. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2308. int ret;
  2309. i915_verify_inactive(dev, __FILE__, __LINE__);
  2310. if (obj_priv->gtt_space == NULL) {
  2311. ret = i915_gem_object_bind_to_gtt(obj, alignment);
  2312. if (ret != 0) {
  2313. if (ret != -EBUSY && ret != -ERESTARTSYS)
  2314. DRM_ERROR("Failure to bind: %d", ret);
  2315. return ret;
  2316. }
  2317. /*
  2318. * Pre-965 chips need a fence register set up in order to
  2319. * properly handle tiled surfaces.
  2320. */
  2321. if (!IS_I965G(dev) &&
  2322. obj_priv->fence_reg == I915_FENCE_REG_NONE &&
  2323. obj_priv->tiling_mode != I915_TILING_NONE)
  2324. i915_gem_object_get_fence_reg(obj, true);
  2325. }
  2326. obj_priv->pin_count++;
  2327. /* If the object is not active and not pending a flush,
  2328. * remove it from the inactive list
  2329. */
  2330. if (obj_priv->pin_count == 1) {
  2331. atomic_inc(&dev->pin_count);
  2332. atomic_add(obj->size, &dev->pin_memory);
  2333. if (!obj_priv->active &&
  2334. (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
  2335. I915_GEM_DOMAIN_GTT)) == 0 &&
  2336. !list_empty(&obj_priv->list))
  2337. list_del_init(&obj_priv->list);
  2338. }
  2339. i915_verify_inactive(dev, __FILE__, __LINE__);
  2340. return 0;
  2341. }
  2342. void
  2343. i915_gem_object_unpin(struct drm_gem_object *obj)
  2344. {
  2345. struct drm_device *dev = obj->dev;
  2346. drm_i915_private_t *dev_priv = dev->dev_private;
  2347. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2348. i915_verify_inactive(dev, __FILE__, __LINE__);
  2349. obj_priv->pin_count--;
  2350. BUG_ON(obj_priv->pin_count < 0);
  2351. BUG_ON(obj_priv->gtt_space == NULL);
  2352. /* If the object is no longer pinned, and is
  2353. * neither active nor being flushed, then stick it on
  2354. * the inactive list
  2355. */
  2356. if (obj_priv->pin_count == 0) {
  2357. if (!obj_priv->active &&
  2358. (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
  2359. I915_GEM_DOMAIN_GTT)) == 0)
  2360. list_move_tail(&obj_priv->list,
  2361. &dev_priv->mm.inactive_list);
  2362. atomic_dec(&dev->pin_count);
  2363. atomic_sub(obj->size, &dev->pin_memory);
  2364. }
  2365. i915_verify_inactive(dev, __FILE__, __LINE__);
  2366. }
  2367. int
  2368. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  2369. struct drm_file *file_priv)
  2370. {
  2371. struct drm_i915_gem_pin *args = data;
  2372. struct drm_gem_object *obj;
  2373. struct drm_i915_gem_object *obj_priv;
  2374. int ret;
  2375. mutex_lock(&dev->struct_mutex);
  2376. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  2377. if (obj == NULL) {
  2378. DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
  2379. args->handle);
  2380. mutex_unlock(&dev->struct_mutex);
  2381. return -EBADF;
  2382. }
  2383. obj_priv = obj->driver_private;
  2384. if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
  2385. DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
  2386. args->handle);
  2387. mutex_unlock(&dev->struct_mutex);
  2388. return -EINVAL;
  2389. }
  2390. obj_priv->user_pin_count++;
  2391. obj_priv->pin_filp = file_priv;
  2392. if (obj_priv->user_pin_count == 1) {
  2393. ret = i915_gem_object_pin(obj, args->alignment);
  2394. if (ret != 0) {
  2395. drm_gem_object_unreference(obj);
  2396. mutex_unlock(&dev->struct_mutex);
  2397. return ret;
  2398. }
  2399. }
  2400. /* XXX - flush the CPU caches for pinned objects
  2401. * as the X server doesn't manage domains yet
  2402. */
  2403. i915_gem_object_flush_cpu_write_domain(obj);
  2404. args->offset = obj_priv->gtt_offset;
  2405. drm_gem_object_unreference(obj);
  2406. mutex_unlock(&dev->struct_mutex);
  2407. return 0;
  2408. }
  2409. int
  2410. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  2411. struct drm_file *file_priv)
  2412. {
  2413. struct drm_i915_gem_pin *args = data;
  2414. struct drm_gem_object *obj;
  2415. struct drm_i915_gem_object *obj_priv;
  2416. mutex_lock(&dev->struct_mutex);
  2417. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  2418. if (obj == NULL) {
  2419. DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
  2420. args->handle);
  2421. mutex_unlock(&dev->struct_mutex);
  2422. return -EBADF;
  2423. }
  2424. obj_priv = obj->driver_private;
  2425. if (obj_priv->pin_filp != file_priv) {
  2426. DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  2427. args->handle);
  2428. drm_gem_object_unreference(obj);
  2429. mutex_unlock(&dev->struct_mutex);
  2430. return -EINVAL;
  2431. }
  2432. obj_priv->user_pin_count--;
  2433. if (obj_priv->user_pin_count == 0) {
  2434. obj_priv->pin_filp = NULL;
  2435. i915_gem_object_unpin(obj);
  2436. }
  2437. drm_gem_object_unreference(obj);
  2438. mutex_unlock(&dev->struct_mutex);
  2439. return 0;
  2440. }
  2441. int
  2442. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  2443. struct drm_file *file_priv)
  2444. {
  2445. struct drm_i915_gem_busy *args = data;
  2446. struct drm_gem_object *obj;
  2447. struct drm_i915_gem_object *obj_priv;
  2448. mutex_lock(&dev->struct_mutex);
  2449. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  2450. if (obj == NULL) {
  2451. DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
  2452. args->handle);
  2453. mutex_unlock(&dev->struct_mutex);
  2454. return -EBADF;
  2455. }
  2456. obj_priv = obj->driver_private;
  2457. /* Don't count being on the flushing list against the object being
  2458. * done. Otherwise, a buffer left on the flushing list but not getting
  2459. * flushed (because nobody's flushing that domain) won't ever return
  2460. * unbusy and get reused by libdrm's bo cache. The other expected
  2461. * consumer of this interface, OpenGL's occlusion queries, also specs
  2462. * that the objects get unbusy "eventually" without any interference.
  2463. */
  2464. args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
  2465. drm_gem_object_unreference(obj);
  2466. mutex_unlock(&dev->struct_mutex);
  2467. return 0;
  2468. }
  2469. int
  2470. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  2471. struct drm_file *file_priv)
  2472. {
  2473. return i915_gem_ring_throttle(dev, file_priv);
  2474. }
  2475. int i915_gem_init_object(struct drm_gem_object *obj)
  2476. {
  2477. struct drm_i915_gem_object *obj_priv;
  2478. obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
  2479. if (obj_priv == NULL)
  2480. return -ENOMEM;
  2481. /*
  2482. * We've just allocated pages from the kernel,
  2483. * so they've just been written by the CPU with
  2484. * zeros. They'll need to be clflushed before we
  2485. * use them with the GPU.
  2486. */
  2487. obj->write_domain = I915_GEM_DOMAIN_CPU;
  2488. obj->read_domains = I915_GEM_DOMAIN_CPU;
  2489. obj_priv->agp_type = AGP_USER_MEMORY;
  2490. obj->driver_private = obj_priv;
  2491. obj_priv->obj = obj;
  2492. obj_priv->fence_reg = I915_FENCE_REG_NONE;
  2493. INIT_LIST_HEAD(&obj_priv->list);
  2494. return 0;
  2495. }
  2496. void i915_gem_free_object(struct drm_gem_object *obj)
  2497. {
  2498. struct drm_device *dev = obj->dev;
  2499. struct drm_gem_mm *mm = dev->mm_private;
  2500. struct drm_map_list *list;
  2501. struct drm_map *map;
  2502. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2503. while (obj_priv->pin_count > 0)
  2504. i915_gem_object_unpin(obj);
  2505. if (obj_priv->phys_obj)
  2506. i915_gem_detach_phys_object(dev, obj);
  2507. i915_gem_object_unbind(obj);
  2508. list = &obj->map_list;
  2509. drm_ht_remove_item(&mm->offset_hash, &list->hash);
  2510. if (list->file_offset_node) {
  2511. drm_mm_put_block(list->file_offset_node);
  2512. list->file_offset_node = NULL;
  2513. }
  2514. map = list->map;
  2515. if (map) {
  2516. drm_free(map, sizeof(*map), DRM_MEM_DRIVER);
  2517. list->map = NULL;
  2518. }
  2519. drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
  2520. drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
  2521. }
  2522. /** Unbinds all objects that are on the given buffer list. */
  2523. static int
  2524. i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
  2525. {
  2526. struct drm_gem_object *obj;
  2527. struct drm_i915_gem_object *obj_priv;
  2528. int ret;
  2529. while (!list_empty(head)) {
  2530. obj_priv = list_first_entry(head,
  2531. struct drm_i915_gem_object,
  2532. list);
  2533. obj = obj_priv->obj;
  2534. if (obj_priv->pin_count != 0) {
  2535. DRM_ERROR("Pinned object in unbind list\n");
  2536. mutex_unlock(&dev->struct_mutex);
  2537. return -EINVAL;
  2538. }
  2539. ret = i915_gem_object_unbind(obj);
  2540. if (ret != 0) {
  2541. DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
  2542. ret);
  2543. mutex_unlock(&dev->struct_mutex);
  2544. return ret;
  2545. }
  2546. }
  2547. return 0;
  2548. }
  2549. static int
  2550. i915_gem_idle(struct drm_device *dev)
  2551. {
  2552. drm_i915_private_t *dev_priv = dev->dev_private;
  2553. uint32_t seqno, cur_seqno, last_seqno;
  2554. int stuck, ret;
  2555. mutex_lock(&dev->struct_mutex);
  2556. if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
  2557. mutex_unlock(&dev->struct_mutex);
  2558. return 0;
  2559. }
  2560. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  2561. * We need to replace this with a semaphore, or something.
  2562. */
  2563. dev_priv->mm.suspended = 1;
  2564. /* Cancel the retire work handler, wait for it to finish if running
  2565. */
  2566. mutex_unlock(&dev->struct_mutex);
  2567. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  2568. mutex_lock(&dev->struct_mutex);
  2569. i915_kernel_lost_context(dev);
  2570. /* Flush the GPU along with all non-CPU write domains
  2571. */
  2572. i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT),
  2573. ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  2574. seqno = i915_add_request(dev, ~I915_GEM_DOMAIN_CPU);
  2575. if (seqno == 0) {
  2576. mutex_unlock(&dev->struct_mutex);
  2577. return -ENOMEM;
  2578. }
  2579. dev_priv->mm.waiting_gem_seqno = seqno;
  2580. last_seqno = 0;
  2581. stuck = 0;
  2582. for (;;) {
  2583. cur_seqno = i915_get_gem_seqno(dev);
  2584. if (i915_seqno_passed(cur_seqno, seqno))
  2585. break;
  2586. if (last_seqno == cur_seqno) {
  2587. if (stuck++ > 100) {
  2588. DRM_ERROR("hardware wedged\n");
  2589. dev_priv->mm.wedged = 1;
  2590. DRM_WAKEUP(&dev_priv->irq_queue);
  2591. break;
  2592. }
  2593. }
  2594. msleep(10);
  2595. last_seqno = cur_seqno;
  2596. }
  2597. dev_priv->mm.waiting_gem_seqno = 0;
  2598. i915_gem_retire_requests(dev);
  2599. if (!dev_priv->mm.wedged) {
  2600. /* Active and flushing should now be empty as we've
  2601. * waited for a sequence higher than any pending execbuffer
  2602. */
  2603. WARN_ON(!list_empty(&dev_priv->mm.active_list));
  2604. WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
  2605. /* Request should now be empty as we've also waited
  2606. * for the last request in the list
  2607. */
  2608. WARN_ON(!list_empty(&dev_priv->mm.request_list));
  2609. }
  2610. /* Empty the active and flushing lists to inactive. If there's
  2611. * anything left at this point, it means that we're wedged and
  2612. * nothing good's going to happen by leaving them there. So strip
  2613. * the GPU domains and just stuff them onto inactive.
  2614. */
  2615. while (!list_empty(&dev_priv->mm.active_list)) {
  2616. struct drm_i915_gem_object *obj_priv;
  2617. obj_priv = list_first_entry(&dev_priv->mm.active_list,
  2618. struct drm_i915_gem_object,
  2619. list);
  2620. obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
  2621. i915_gem_object_move_to_inactive(obj_priv->obj);
  2622. }
  2623. while (!list_empty(&dev_priv->mm.flushing_list)) {
  2624. struct drm_i915_gem_object *obj_priv;
  2625. obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
  2626. struct drm_i915_gem_object,
  2627. list);
  2628. obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
  2629. i915_gem_object_move_to_inactive(obj_priv->obj);
  2630. }
  2631. /* Move all inactive buffers out of the GTT. */
  2632. ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
  2633. WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
  2634. if (ret) {
  2635. mutex_unlock(&dev->struct_mutex);
  2636. return ret;
  2637. }
  2638. i915_gem_cleanup_ringbuffer(dev);
  2639. mutex_unlock(&dev->struct_mutex);
  2640. return 0;
  2641. }
  2642. static int
  2643. i915_gem_init_hws(struct drm_device *dev)
  2644. {
  2645. drm_i915_private_t *dev_priv = dev->dev_private;
  2646. struct drm_gem_object *obj;
  2647. struct drm_i915_gem_object *obj_priv;
  2648. int ret;
  2649. /* If we need a physical address for the status page, it's already
  2650. * initialized at driver load time.
  2651. */
  2652. if (!I915_NEED_GFX_HWS(dev))
  2653. return 0;
  2654. obj = drm_gem_object_alloc(dev, 4096);
  2655. if (obj == NULL) {
  2656. DRM_ERROR("Failed to allocate status page\n");
  2657. return -ENOMEM;
  2658. }
  2659. obj_priv = obj->driver_private;
  2660. obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
  2661. ret = i915_gem_object_pin(obj, 4096);
  2662. if (ret != 0) {
  2663. drm_gem_object_unreference(obj);
  2664. return ret;
  2665. }
  2666. dev_priv->status_gfx_addr = obj_priv->gtt_offset;
  2667. dev_priv->hw_status_page = kmap(obj_priv->page_list[0]);
  2668. if (dev_priv->hw_status_page == NULL) {
  2669. DRM_ERROR("Failed to map status page.\n");
  2670. memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
  2671. drm_gem_object_unreference(obj);
  2672. return -EINVAL;
  2673. }
  2674. dev_priv->hws_obj = obj;
  2675. memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
  2676. I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
  2677. I915_READ(HWS_PGA); /* posting read */
  2678. DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
  2679. return 0;
  2680. }
  2681. int
  2682. i915_gem_init_ringbuffer(struct drm_device *dev)
  2683. {
  2684. drm_i915_private_t *dev_priv = dev->dev_private;
  2685. struct drm_gem_object *obj;
  2686. struct drm_i915_gem_object *obj_priv;
  2687. drm_i915_ring_buffer_t *ring = &dev_priv->ring;
  2688. int ret;
  2689. u32 head;
  2690. ret = i915_gem_init_hws(dev);
  2691. if (ret != 0)
  2692. return ret;
  2693. obj = drm_gem_object_alloc(dev, 128 * 1024);
  2694. if (obj == NULL) {
  2695. DRM_ERROR("Failed to allocate ringbuffer\n");
  2696. return -ENOMEM;
  2697. }
  2698. obj_priv = obj->driver_private;
  2699. ret = i915_gem_object_pin(obj, 4096);
  2700. if (ret != 0) {
  2701. drm_gem_object_unreference(obj);
  2702. return ret;
  2703. }
  2704. /* Set up the kernel mapping for the ring. */
  2705. ring->Size = obj->size;
  2706. ring->tail_mask = obj->size - 1;
  2707. ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
  2708. ring->map.size = obj->size;
  2709. ring->map.type = 0;
  2710. ring->map.flags = 0;
  2711. ring->map.mtrr = 0;
  2712. drm_core_ioremap_wc(&ring->map, dev);
  2713. if (ring->map.handle == NULL) {
  2714. DRM_ERROR("Failed to map ringbuffer.\n");
  2715. memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
  2716. drm_gem_object_unreference(obj);
  2717. return -EINVAL;
  2718. }
  2719. ring->ring_obj = obj;
  2720. ring->virtual_start = ring->map.handle;
  2721. /* Stop the ring if it's running. */
  2722. I915_WRITE(PRB0_CTL, 0);
  2723. I915_WRITE(PRB0_TAIL, 0);
  2724. I915_WRITE(PRB0_HEAD, 0);
  2725. /* Initialize the ring. */
  2726. I915_WRITE(PRB0_START, obj_priv->gtt_offset);
  2727. head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  2728. /* G45 ring initialization fails to reset head to zero */
  2729. if (head != 0) {
  2730. DRM_ERROR("Ring head not reset to zero "
  2731. "ctl %08x head %08x tail %08x start %08x\n",
  2732. I915_READ(PRB0_CTL),
  2733. I915_READ(PRB0_HEAD),
  2734. I915_READ(PRB0_TAIL),
  2735. I915_READ(PRB0_START));
  2736. I915_WRITE(PRB0_HEAD, 0);
  2737. DRM_ERROR("Ring head forced to zero "
  2738. "ctl %08x head %08x tail %08x start %08x\n",
  2739. I915_READ(PRB0_CTL),
  2740. I915_READ(PRB0_HEAD),
  2741. I915_READ(PRB0_TAIL),
  2742. I915_READ(PRB0_START));
  2743. }
  2744. I915_WRITE(PRB0_CTL,
  2745. ((obj->size - 4096) & RING_NR_PAGES) |
  2746. RING_NO_REPORT |
  2747. RING_VALID);
  2748. head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  2749. /* If the head is still not zero, the ring is dead */
  2750. if (head != 0) {
  2751. DRM_ERROR("Ring initialization failed "
  2752. "ctl %08x head %08x tail %08x start %08x\n",
  2753. I915_READ(PRB0_CTL),
  2754. I915_READ(PRB0_HEAD),
  2755. I915_READ(PRB0_TAIL),
  2756. I915_READ(PRB0_START));
  2757. return -EIO;
  2758. }
  2759. /* Update our cache of the ring state */
  2760. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2761. i915_kernel_lost_context(dev);
  2762. else {
  2763. ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  2764. ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
  2765. ring->space = ring->head - (ring->tail + 8);
  2766. if (ring->space < 0)
  2767. ring->space += ring->Size;
  2768. }
  2769. return 0;
  2770. }
  2771. void
  2772. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  2773. {
  2774. drm_i915_private_t *dev_priv = dev->dev_private;
  2775. if (dev_priv->ring.ring_obj == NULL)
  2776. return;
  2777. drm_core_ioremapfree(&dev_priv->ring.map, dev);
  2778. i915_gem_object_unpin(dev_priv->ring.ring_obj);
  2779. drm_gem_object_unreference(dev_priv->ring.ring_obj);
  2780. dev_priv->ring.ring_obj = NULL;
  2781. memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
  2782. if (dev_priv->hws_obj != NULL) {
  2783. struct drm_gem_object *obj = dev_priv->hws_obj;
  2784. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2785. kunmap(obj_priv->page_list[0]);
  2786. i915_gem_object_unpin(obj);
  2787. drm_gem_object_unreference(obj);
  2788. dev_priv->hws_obj = NULL;
  2789. memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
  2790. dev_priv->hw_status_page = NULL;
  2791. /* Write high address into HWS_PGA when disabling. */
  2792. I915_WRITE(HWS_PGA, 0x1ffff000);
  2793. }
  2794. }
  2795. int
  2796. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  2797. struct drm_file *file_priv)
  2798. {
  2799. drm_i915_private_t *dev_priv = dev->dev_private;
  2800. int ret;
  2801. if (drm_core_check_feature(dev, DRIVER_MODESET))
  2802. return 0;
  2803. if (dev_priv->mm.wedged) {
  2804. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  2805. dev_priv->mm.wedged = 0;
  2806. }
  2807. mutex_lock(&dev->struct_mutex);
  2808. dev_priv->mm.suspended = 0;
  2809. ret = i915_gem_init_ringbuffer(dev);
  2810. if (ret != 0)
  2811. return ret;
  2812. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  2813. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  2814. BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
  2815. BUG_ON(!list_empty(&dev_priv->mm.request_list));
  2816. mutex_unlock(&dev->struct_mutex);
  2817. drm_irq_install(dev);
  2818. return 0;
  2819. }
  2820. int
  2821. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  2822. struct drm_file *file_priv)
  2823. {
  2824. int ret;
  2825. if (drm_core_check_feature(dev, DRIVER_MODESET))
  2826. return 0;
  2827. ret = i915_gem_idle(dev);
  2828. drm_irq_uninstall(dev);
  2829. return ret;
  2830. }
  2831. void
  2832. i915_gem_lastclose(struct drm_device *dev)
  2833. {
  2834. int ret;
  2835. if (drm_core_check_feature(dev, DRIVER_MODESET))
  2836. return;
  2837. ret = i915_gem_idle(dev);
  2838. if (ret)
  2839. DRM_ERROR("failed to idle hardware: %d\n", ret);
  2840. }
  2841. void
  2842. i915_gem_load(struct drm_device *dev)
  2843. {
  2844. drm_i915_private_t *dev_priv = dev->dev_private;
  2845. INIT_LIST_HEAD(&dev_priv->mm.active_list);
  2846. INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
  2847. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  2848. INIT_LIST_HEAD(&dev_priv->mm.request_list);
  2849. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  2850. i915_gem_retire_work_handler);
  2851. dev_priv->mm.next_gem_seqno = 1;
  2852. /* Old X drivers will take 0-2 for front, back, depth buffers */
  2853. dev_priv->fence_reg_start = 3;
  2854. if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  2855. dev_priv->num_fence_regs = 16;
  2856. else
  2857. dev_priv->num_fence_regs = 8;
  2858. i915_gem_detect_bit_6_swizzle(dev);
  2859. }
  2860. /*
  2861. * Create a physically contiguous memory object for this object
  2862. * e.g. for cursor + overlay regs
  2863. */
  2864. int i915_gem_init_phys_object(struct drm_device *dev,
  2865. int id, int size)
  2866. {
  2867. drm_i915_private_t *dev_priv = dev->dev_private;
  2868. struct drm_i915_gem_phys_object *phys_obj;
  2869. int ret;
  2870. if (dev_priv->mm.phys_objs[id - 1] || !size)
  2871. return 0;
  2872. phys_obj = drm_calloc(1, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
  2873. if (!phys_obj)
  2874. return -ENOMEM;
  2875. phys_obj->id = id;
  2876. phys_obj->handle = drm_pci_alloc(dev, size, 0, 0xffffffff);
  2877. if (!phys_obj->handle) {
  2878. ret = -ENOMEM;
  2879. goto kfree_obj;
  2880. }
  2881. #ifdef CONFIG_X86
  2882. set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  2883. #endif
  2884. dev_priv->mm.phys_objs[id - 1] = phys_obj;
  2885. return 0;
  2886. kfree_obj:
  2887. drm_free(phys_obj, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
  2888. return ret;
  2889. }
  2890. void i915_gem_free_phys_object(struct drm_device *dev, int id)
  2891. {
  2892. drm_i915_private_t *dev_priv = dev->dev_private;
  2893. struct drm_i915_gem_phys_object *phys_obj;
  2894. if (!dev_priv->mm.phys_objs[id - 1])
  2895. return;
  2896. phys_obj = dev_priv->mm.phys_objs[id - 1];
  2897. if (phys_obj->cur_obj) {
  2898. i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
  2899. }
  2900. #ifdef CONFIG_X86
  2901. set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  2902. #endif
  2903. drm_pci_free(dev, phys_obj->handle);
  2904. kfree(phys_obj);
  2905. dev_priv->mm.phys_objs[id - 1] = NULL;
  2906. }
  2907. void i915_gem_free_all_phys_object(struct drm_device *dev)
  2908. {
  2909. int i;
  2910. for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
  2911. i915_gem_free_phys_object(dev, i);
  2912. }
  2913. void i915_gem_detach_phys_object(struct drm_device *dev,
  2914. struct drm_gem_object *obj)
  2915. {
  2916. struct drm_i915_gem_object *obj_priv;
  2917. int i;
  2918. int ret;
  2919. int page_count;
  2920. obj_priv = obj->driver_private;
  2921. if (!obj_priv->phys_obj)
  2922. return;
  2923. ret = i915_gem_object_get_page_list(obj);
  2924. if (ret)
  2925. goto out;
  2926. page_count = obj->size / PAGE_SIZE;
  2927. for (i = 0; i < page_count; i++) {
  2928. char *dst = kmap_atomic(obj_priv->page_list[i], KM_USER0);
  2929. char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  2930. memcpy(dst, src, PAGE_SIZE);
  2931. kunmap_atomic(dst, KM_USER0);
  2932. }
  2933. drm_clflush_pages(obj_priv->page_list, page_count);
  2934. drm_agp_chipset_flush(dev);
  2935. out:
  2936. obj_priv->phys_obj->cur_obj = NULL;
  2937. obj_priv->phys_obj = NULL;
  2938. }
  2939. int
  2940. i915_gem_attach_phys_object(struct drm_device *dev,
  2941. struct drm_gem_object *obj, int id)
  2942. {
  2943. drm_i915_private_t *dev_priv = dev->dev_private;
  2944. struct drm_i915_gem_object *obj_priv;
  2945. int ret = 0;
  2946. int page_count;
  2947. int i;
  2948. if (id > I915_MAX_PHYS_OBJECT)
  2949. return -EINVAL;
  2950. obj_priv = obj->driver_private;
  2951. if (obj_priv->phys_obj) {
  2952. if (obj_priv->phys_obj->id == id)
  2953. return 0;
  2954. i915_gem_detach_phys_object(dev, obj);
  2955. }
  2956. /* create a new object */
  2957. if (!dev_priv->mm.phys_objs[id - 1]) {
  2958. ret = i915_gem_init_phys_object(dev, id,
  2959. obj->size);
  2960. if (ret) {
  2961. DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
  2962. goto out;
  2963. }
  2964. }
  2965. /* bind to the object */
  2966. obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
  2967. obj_priv->phys_obj->cur_obj = obj;
  2968. ret = i915_gem_object_get_page_list(obj);
  2969. if (ret) {
  2970. DRM_ERROR("failed to get page list\n");
  2971. goto out;
  2972. }
  2973. page_count = obj->size / PAGE_SIZE;
  2974. for (i = 0; i < page_count; i++) {
  2975. char *src = kmap_atomic(obj_priv->page_list[i], KM_USER0);
  2976. char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  2977. memcpy(dst, src, PAGE_SIZE);
  2978. kunmap_atomic(src, KM_USER0);
  2979. }
  2980. return 0;
  2981. out:
  2982. return ret;
  2983. }
  2984. static int
  2985. i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  2986. struct drm_i915_gem_pwrite *args,
  2987. struct drm_file *file_priv)
  2988. {
  2989. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2990. void *obj_addr;
  2991. int ret;
  2992. char __user *user_data;
  2993. user_data = (char __user *) (uintptr_t) args->data_ptr;
  2994. obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
  2995. DRM_ERROR("obj_addr %p, %lld\n", obj_addr, args->size);
  2996. ret = copy_from_user(obj_addr, user_data, args->size);
  2997. if (ret)
  2998. return -EFAULT;
  2999. drm_agp_chipset_flush(dev);
  3000. return 0;
  3001. }