i915_gem.c 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034
  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 "i915_trace.h"
  32. #include "intel_drv.h"
  33. #include <linux/shmem_fs.h>
  34. #include <linux/slab.h>
  35. #include <linux/swap.h>
  36. #include <linux/pci.h>
  37. #include <linux/dma-buf.h>
  38. static __must_check int i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj);
  39. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  40. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
  41. static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
  42. unsigned alignment,
  43. bool map_and_fenceable);
  44. static int i915_gem_phys_pwrite(struct drm_device *dev,
  45. struct drm_i915_gem_object *obj,
  46. struct drm_i915_gem_pwrite *args,
  47. struct drm_file *file);
  48. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  49. struct drm_i915_gem_object *obj);
  50. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  51. struct drm_i915_fence_reg *fence,
  52. bool enable);
  53. static int i915_gem_inactive_shrink(struct shrinker *shrinker,
  54. struct shrink_control *sc);
  55. static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
  56. static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
  57. {
  58. if (obj->tiling_mode)
  59. i915_gem_release_mmap(obj);
  60. /* As we do not have an associated fence register, we will force
  61. * a tiling change if we ever need to acquire one.
  62. */
  63. obj->fence_dirty = false;
  64. obj->fence_reg = I915_FENCE_REG_NONE;
  65. }
  66. /* some bookkeeping */
  67. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  68. size_t size)
  69. {
  70. dev_priv->mm.object_count++;
  71. dev_priv->mm.object_memory += size;
  72. }
  73. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  74. size_t size)
  75. {
  76. dev_priv->mm.object_count--;
  77. dev_priv->mm.object_memory -= size;
  78. }
  79. static int
  80. i915_gem_wait_for_error(struct drm_device *dev)
  81. {
  82. struct drm_i915_private *dev_priv = dev->dev_private;
  83. struct completion *x = &dev_priv->error_completion;
  84. unsigned long flags;
  85. int ret;
  86. if (!atomic_read(&dev_priv->mm.wedged))
  87. return 0;
  88. ret = wait_for_completion_interruptible(x);
  89. if (ret)
  90. return ret;
  91. if (atomic_read(&dev_priv->mm.wedged)) {
  92. /* GPU is hung, bump the completion count to account for
  93. * the token we just consumed so that we never hit zero and
  94. * end up waiting upon a subsequent completion event that
  95. * will never happen.
  96. */
  97. spin_lock_irqsave(&x->wait.lock, flags);
  98. x->done++;
  99. spin_unlock_irqrestore(&x->wait.lock, flags);
  100. }
  101. return 0;
  102. }
  103. int i915_mutex_lock_interruptible(struct drm_device *dev)
  104. {
  105. int ret;
  106. ret = i915_gem_wait_for_error(dev);
  107. if (ret)
  108. return ret;
  109. ret = mutex_lock_interruptible(&dev->struct_mutex);
  110. if (ret)
  111. return ret;
  112. WARN_ON(i915_verify_lists(dev));
  113. return 0;
  114. }
  115. static inline bool
  116. i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
  117. {
  118. return !obj->active;
  119. }
  120. int
  121. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  122. struct drm_file *file)
  123. {
  124. struct drm_i915_gem_init *args = data;
  125. if (drm_core_check_feature(dev, DRIVER_MODESET))
  126. return -ENODEV;
  127. if (args->gtt_start >= args->gtt_end ||
  128. (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
  129. return -EINVAL;
  130. /* GEM with user mode setting was never supported on ilk and later. */
  131. if (INTEL_INFO(dev)->gen >= 5)
  132. return -ENODEV;
  133. mutex_lock(&dev->struct_mutex);
  134. i915_gem_init_global_gtt(dev, args->gtt_start,
  135. args->gtt_end, args->gtt_end);
  136. mutex_unlock(&dev->struct_mutex);
  137. return 0;
  138. }
  139. int
  140. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  141. struct drm_file *file)
  142. {
  143. struct drm_i915_private *dev_priv = dev->dev_private;
  144. struct drm_i915_gem_get_aperture *args = data;
  145. struct drm_i915_gem_object *obj;
  146. size_t pinned;
  147. pinned = 0;
  148. mutex_lock(&dev->struct_mutex);
  149. list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list)
  150. if (obj->pin_count)
  151. pinned += obj->gtt_space->size;
  152. mutex_unlock(&dev->struct_mutex);
  153. args->aper_size = dev_priv->mm.gtt_total;
  154. args->aper_available_size = args->aper_size - pinned;
  155. return 0;
  156. }
  157. static int
  158. i915_gem_create(struct drm_file *file,
  159. struct drm_device *dev,
  160. uint64_t size,
  161. uint32_t *handle_p)
  162. {
  163. struct drm_i915_gem_object *obj;
  164. int ret;
  165. u32 handle;
  166. size = roundup(size, PAGE_SIZE);
  167. if (size == 0)
  168. return -EINVAL;
  169. /* Allocate the new object */
  170. obj = i915_gem_alloc_object(dev, size);
  171. if (obj == NULL)
  172. return -ENOMEM;
  173. ret = drm_gem_handle_create(file, &obj->base, &handle);
  174. if (ret) {
  175. drm_gem_object_release(&obj->base);
  176. i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
  177. kfree(obj);
  178. return ret;
  179. }
  180. /* drop reference from allocate - handle holds it now */
  181. drm_gem_object_unreference(&obj->base);
  182. trace_i915_gem_object_create(obj);
  183. *handle_p = handle;
  184. return 0;
  185. }
  186. int
  187. i915_gem_dumb_create(struct drm_file *file,
  188. struct drm_device *dev,
  189. struct drm_mode_create_dumb *args)
  190. {
  191. /* have to work out size/pitch and return them */
  192. args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
  193. args->size = args->pitch * args->height;
  194. return i915_gem_create(file, dev,
  195. args->size, &args->handle);
  196. }
  197. int i915_gem_dumb_destroy(struct drm_file *file,
  198. struct drm_device *dev,
  199. uint32_t handle)
  200. {
  201. return drm_gem_handle_delete(file, handle);
  202. }
  203. /**
  204. * Creates a new mm object and returns a handle to it.
  205. */
  206. int
  207. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  208. struct drm_file *file)
  209. {
  210. struct drm_i915_gem_create *args = data;
  211. return i915_gem_create(file, dev,
  212. args->size, &args->handle);
  213. }
  214. static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
  215. {
  216. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  217. return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
  218. obj->tiling_mode != I915_TILING_NONE;
  219. }
  220. static inline int
  221. __copy_to_user_swizzled(char __user *cpu_vaddr,
  222. const char *gpu_vaddr, int gpu_offset,
  223. int length)
  224. {
  225. int ret, cpu_offset = 0;
  226. while (length > 0) {
  227. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  228. int this_length = min(cacheline_end - gpu_offset, length);
  229. int swizzled_gpu_offset = gpu_offset ^ 64;
  230. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  231. gpu_vaddr + swizzled_gpu_offset,
  232. this_length);
  233. if (ret)
  234. return ret + length;
  235. cpu_offset += this_length;
  236. gpu_offset += this_length;
  237. length -= this_length;
  238. }
  239. return 0;
  240. }
  241. static inline int
  242. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  243. const char __user *cpu_vaddr,
  244. int length)
  245. {
  246. int ret, cpu_offset = 0;
  247. while (length > 0) {
  248. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  249. int this_length = min(cacheline_end - gpu_offset, length);
  250. int swizzled_gpu_offset = gpu_offset ^ 64;
  251. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  252. cpu_vaddr + cpu_offset,
  253. this_length);
  254. if (ret)
  255. return ret + length;
  256. cpu_offset += this_length;
  257. gpu_offset += this_length;
  258. length -= this_length;
  259. }
  260. return 0;
  261. }
  262. /* Per-page copy function for the shmem pread fastpath.
  263. * Flushes invalid cachelines before reading the target if
  264. * needs_clflush is set. */
  265. static int
  266. shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
  267. char __user *user_data,
  268. bool page_do_bit17_swizzling, bool needs_clflush)
  269. {
  270. char *vaddr;
  271. int ret;
  272. if (unlikely(page_do_bit17_swizzling))
  273. return -EINVAL;
  274. vaddr = kmap_atomic(page);
  275. if (needs_clflush)
  276. drm_clflush_virt_range(vaddr + shmem_page_offset,
  277. page_length);
  278. ret = __copy_to_user_inatomic(user_data,
  279. vaddr + shmem_page_offset,
  280. page_length);
  281. kunmap_atomic(vaddr);
  282. return ret;
  283. }
  284. static void
  285. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  286. bool swizzled)
  287. {
  288. if (unlikely(swizzled)) {
  289. unsigned long start = (unsigned long) addr;
  290. unsigned long end = (unsigned long) addr + length;
  291. /* For swizzling simply ensure that we always flush both
  292. * channels. Lame, but simple and it works. Swizzled
  293. * pwrite/pread is far from a hotpath - current userspace
  294. * doesn't use it at all. */
  295. start = round_down(start, 128);
  296. end = round_up(end, 128);
  297. drm_clflush_virt_range((void *)start, end - start);
  298. } else {
  299. drm_clflush_virt_range(addr, length);
  300. }
  301. }
  302. /* Only difference to the fast-path function is that this can handle bit17
  303. * and uses non-atomic copy and kmap functions. */
  304. static int
  305. shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
  306. char __user *user_data,
  307. bool page_do_bit17_swizzling, bool needs_clflush)
  308. {
  309. char *vaddr;
  310. int ret;
  311. vaddr = kmap(page);
  312. if (needs_clflush)
  313. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  314. page_length,
  315. page_do_bit17_swizzling);
  316. if (page_do_bit17_swizzling)
  317. ret = __copy_to_user_swizzled(user_data,
  318. vaddr, shmem_page_offset,
  319. page_length);
  320. else
  321. ret = __copy_to_user(user_data,
  322. vaddr + shmem_page_offset,
  323. page_length);
  324. kunmap(page);
  325. return ret;
  326. }
  327. static int
  328. i915_gem_shmem_pread(struct drm_device *dev,
  329. struct drm_i915_gem_object *obj,
  330. struct drm_i915_gem_pread *args,
  331. struct drm_file *file)
  332. {
  333. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  334. char __user *user_data;
  335. ssize_t remain;
  336. loff_t offset;
  337. int shmem_page_offset, page_length, ret = 0;
  338. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  339. int hit_slowpath = 0;
  340. int prefaulted = 0;
  341. int needs_clflush = 0;
  342. int release_page;
  343. user_data = (char __user *) (uintptr_t) args->data_ptr;
  344. remain = args->size;
  345. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  346. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
  347. /* If we're not in the cpu read domain, set ourself into the gtt
  348. * read domain and manually flush cachelines (if required). This
  349. * optimizes for the case when the gpu will dirty the data
  350. * anyway again before the next pread happens. */
  351. if (obj->cache_level == I915_CACHE_NONE)
  352. needs_clflush = 1;
  353. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  354. if (ret)
  355. return ret;
  356. }
  357. offset = args->offset;
  358. while (remain > 0) {
  359. struct page *page;
  360. /* Operation in this page
  361. *
  362. * shmem_page_offset = offset within page in shmem file
  363. * page_length = bytes to copy for this page
  364. */
  365. shmem_page_offset = offset_in_page(offset);
  366. page_length = remain;
  367. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  368. page_length = PAGE_SIZE - shmem_page_offset;
  369. if (obj->pages) {
  370. page = obj->pages[offset >> PAGE_SHIFT];
  371. release_page = 0;
  372. } else {
  373. page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
  374. if (IS_ERR(page)) {
  375. ret = PTR_ERR(page);
  376. goto out;
  377. }
  378. release_page = 1;
  379. }
  380. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  381. (page_to_phys(page) & (1 << 17)) != 0;
  382. ret = shmem_pread_fast(page, shmem_page_offset, page_length,
  383. user_data, page_do_bit17_swizzling,
  384. needs_clflush);
  385. if (ret == 0)
  386. goto next_page;
  387. hit_slowpath = 1;
  388. page_cache_get(page);
  389. mutex_unlock(&dev->struct_mutex);
  390. if (!prefaulted) {
  391. ret = fault_in_multipages_writeable(user_data, remain);
  392. /* Userspace is tricking us, but we've already clobbered
  393. * its pages with the prefault and promised to write the
  394. * data up to the first fault. Hence ignore any errors
  395. * and just continue. */
  396. (void)ret;
  397. prefaulted = 1;
  398. }
  399. ret = shmem_pread_slow(page, shmem_page_offset, page_length,
  400. user_data, page_do_bit17_swizzling,
  401. needs_clflush);
  402. mutex_lock(&dev->struct_mutex);
  403. page_cache_release(page);
  404. next_page:
  405. mark_page_accessed(page);
  406. if (release_page)
  407. page_cache_release(page);
  408. if (ret) {
  409. ret = -EFAULT;
  410. goto out;
  411. }
  412. remain -= page_length;
  413. user_data += page_length;
  414. offset += page_length;
  415. }
  416. out:
  417. if (hit_slowpath) {
  418. /* Fixup: Kill any reinstated backing storage pages */
  419. if (obj->madv == __I915_MADV_PURGED)
  420. i915_gem_object_truncate(obj);
  421. }
  422. return ret;
  423. }
  424. /**
  425. * Reads data from the object referenced by handle.
  426. *
  427. * On error, the contents of *data are undefined.
  428. */
  429. int
  430. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  431. struct drm_file *file)
  432. {
  433. struct drm_i915_gem_pread *args = data;
  434. struct drm_i915_gem_object *obj;
  435. int ret = 0;
  436. if (args->size == 0)
  437. return 0;
  438. if (!access_ok(VERIFY_WRITE,
  439. (char __user *)(uintptr_t)args->data_ptr,
  440. args->size))
  441. return -EFAULT;
  442. ret = i915_mutex_lock_interruptible(dev);
  443. if (ret)
  444. return ret;
  445. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  446. if (&obj->base == NULL) {
  447. ret = -ENOENT;
  448. goto unlock;
  449. }
  450. /* Bounds check source. */
  451. if (args->offset > obj->base.size ||
  452. args->size > obj->base.size - args->offset) {
  453. ret = -EINVAL;
  454. goto out;
  455. }
  456. /* prime objects have no backing filp to GEM pread/pwrite
  457. * pages from.
  458. */
  459. if (!obj->base.filp) {
  460. ret = -EINVAL;
  461. goto out;
  462. }
  463. trace_i915_gem_object_pread(obj, args->offset, args->size);
  464. ret = i915_gem_shmem_pread(dev, obj, args, file);
  465. out:
  466. drm_gem_object_unreference(&obj->base);
  467. unlock:
  468. mutex_unlock(&dev->struct_mutex);
  469. return ret;
  470. }
  471. /* This is the fast write path which cannot handle
  472. * page faults in the source data
  473. */
  474. static inline int
  475. fast_user_write(struct io_mapping *mapping,
  476. loff_t page_base, int page_offset,
  477. char __user *user_data,
  478. int length)
  479. {
  480. void __iomem *vaddr_atomic;
  481. void *vaddr;
  482. unsigned long unwritten;
  483. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  484. /* We can use the cpu mem copy function because this is X86. */
  485. vaddr = (void __force*)vaddr_atomic + page_offset;
  486. unwritten = __copy_from_user_inatomic_nocache(vaddr,
  487. user_data, length);
  488. io_mapping_unmap_atomic(vaddr_atomic);
  489. return unwritten;
  490. }
  491. /**
  492. * This is the fast pwrite path, where we copy the data directly from the
  493. * user into the GTT, uncached.
  494. */
  495. static int
  496. i915_gem_gtt_pwrite_fast(struct drm_device *dev,
  497. struct drm_i915_gem_object *obj,
  498. struct drm_i915_gem_pwrite *args,
  499. struct drm_file *file)
  500. {
  501. drm_i915_private_t *dev_priv = dev->dev_private;
  502. ssize_t remain;
  503. loff_t offset, page_base;
  504. char __user *user_data;
  505. int page_offset, page_length, ret;
  506. ret = i915_gem_object_pin(obj, 0, true);
  507. if (ret)
  508. goto out;
  509. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  510. if (ret)
  511. goto out_unpin;
  512. ret = i915_gem_object_put_fence(obj);
  513. if (ret)
  514. goto out_unpin;
  515. user_data = (char __user *) (uintptr_t) args->data_ptr;
  516. remain = args->size;
  517. offset = obj->gtt_offset + args->offset;
  518. while (remain > 0) {
  519. /* Operation in this page
  520. *
  521. * page_base = page offset within aperture
  522. * page_offset = offset within page
  523. * page_length = bytes to copy for this page
  524. */
  525. page_base = offset & PAGE_MASK;
  526. page_offset = offset_in_page(offset);
  527. page_length = remain;
  528. if ((page_offset + remain) > PAGE_SIZE)
  529. page_length = PAGE_SIZE - page_offset;
  530. /* If we get a fault while copying data, then (presumably) our
  531. * source page isn't available. Return the error and we'll
  532. * retry in the slow path.
  533. */
  534. if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
  535. page_offset, user_data, page_length)) {
  536. ret = -EFAULT;
  537. goto out_unpin;
  538. }
  539. remain -= page_length;
  540. user_data += page_length;
  541. offset += page_length;
  542. }
  543. out_unpin:
  544. i915_gem_object_unpin(obj);
  545. out:
  546. return ret;
  547. }
  548. /* Per-page copy function for the shmem pwrite fastpath.
  549. * Flushes invalid cachelines before writing to the target if
  550. * needs_clflush_before is set and flushes out any written cachelines after
  551. * writing if needs_clflush is set. */
  552. static int
  553. shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
  554. char __user *user_data,
  555. bool page_do_bit17_swizzling,
  556. bool needs_clflush_before,
  557. bool needs_clflush_after)
  558. {
  559. char *vaddr;
  560. int ret;
  561. if (unlikely(page_do_bit17_swizzling))
  562. return -EINVAL;
  563. vaddr = kmap_atomic(page);
  564. if (needs_clflush_before)
  565. drm_clflush_virt_range(vaddr + shmem_page_offset,
  566. page_length);
  567. ret = __copy_from_user_inatomic_nocache(vaddr + shmem_page_offset,
  568. user_data,
  569. page_length);
  570. if (needs_clflush_after)
  571. drm_clflush_virt_range(vaddr + shmem_page_offset,
  572. page_length);
  573. kunmap_atomic(vaddr);
  574. return ret;
  575. }
  576. /* Only difference to the fast-path function is that this can handle bit17
  577. * and uses non-atomic copy and kmap functions. */
  578. static int
  579. shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
  580. char __user *user_data,
  581. bool page_do_bit17_swizzling,
  582. bool needs_clflush_before,
  583. bool needs_clflush_after)
  584. {
  585. char *vaddr;
  586. int ret;
  587. vaddr = kmap(page);
  588. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  589. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  590. page_length,
  591. page_do_bit17_swizzling);
  592. if (page_do_bit17_swizzling)
  593. ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
  594. user_data,
  595. page_length);
  596. else
  597. ret = __copy_from_user(vaddr + shmem_page_offset,
  598. user_data,
  599. page_length);
  600. if (needs_clflush_after)
  601. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  602. page_length,
  603. page_do_bit17_swizzling);
  604. kunmap(page);
  605. return ret;
  606. }
  607. static int
  608. i915_gem_shmem_pwrite(struct drm_device *dev,
  609. struct drm_i915_gem_object *obj,
  610. struct drm_i915_gem_pwrite *args,
  611. struct drm_file *file)
  612. {
  613. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  614. ssize_t remain;
  615. loff_t offset;
  616. char __user *user_data;
  617. int shmem_page_offset, page_length, ret = 0;
  618. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  619. int hit_slowpath = 0;
  620. int needs_clflush_after = 0;
  621. int needs_clflush_before = 0;
  622. int release_page;
  623. user_data = (char __user *) (uintptr_t) args->data_ptr;
  624. remain = args->size;
  625. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  626. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  627. /* If we're not in the cpu write domain, set ourself into the gtt
  628. * write domain and manually flush cachelines (if required). This
  629. * optimizes for the case when the gpu will use the data
  630. * right away and we therefore have to clflush anyway. */
  631. if (obj->cache_level == I915_CACHE_NONE)
  632. needs_clflush_after = 1;
  633. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  634. if (ret)
  635. return ret;
  636. }
  637. /* Same trick applies for invalidate partially written cachelines before
  638. * writing. */
  639. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)
  640. && obj->cache_level == I915_CACHE_NONE)
  641. needs_clflush_before = 1;
  642. offset = args->offset;
  643. obj->dirty = 1;
  644. while (remain > 0) {
  645. struct page *page;
  646. int partial_cacheline_write;
  647. /* Operation in this page
  648. *
  649. * shmem_page_offset = offset within page in shmem file
  650. * page_length = bytes to copy for this page
  651. */
  652. shmem_page_offset = offset_in_page(offset);
  653. page_length = remain;
  654. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  655. page_length = PAGE_SIZE - shmem_page_offset;
  656. /* If we don't overwrite a cacheline completely we need to be
  657. * careful to have up-to-date data by first clflushing. Don't
  658. * overcomplicate things and flush the entire patch. */
  659. partial_cacheline_write = needs_clflush_before &&
  660. ((shmem_page_offset | page_length)
  661. & (boot_cpu_data.x86_clflush_size - 1));
  662. if (obj->pages) {
  663. page = obj->pages[offset >> PAGE_SHIFT];
  664. release_page = 0;
  665. } else {
  666. page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
  667. if (IS_ERR(page)) {
  668. ret = PTR_ERR(page);
  669. goto out;
  670. }
  671. release_page = 1;
  672. }
  673. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  674. (page_to_phys(page) & (1 << 17)) != 0;
  675. ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
  676. user_data, page_do_bit17_swizzling,
  677. partial_cacheline_write,
  678. needs_clflush_after);
  679. if (ret == 0)
  680. goto next_page;
  681. hit_slowpath = 1;
  682. page_cache_get(page);
  683. mutex_unlock(&dev->struct_mutex);
  684. ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
  685. user_data, page_do_bit17_swizzling,
  686. partial_cacheline_write,
  687. needs_clflush_after);
  688. mutex_lock(&dev->struct_mutex);
  689. page_cache_release(page);
  690. next_page:
  691. set_page_dirty(page);
  692. mark_page_accessed(page);
  693. if (release_page)
  694. page_cache_release(page);
  695. if (ret) {
  696. ret = -EFAULT;
  697. goto out;
  698. }
  699. remain -= page_length;
  700. user_data += page_length;
  701. offset += page_length;
  702. }
  703. out:
  704. if (hit_slowpath) {
  705. /* Fixup: Kill any reinstated backing storage pages */
  706. if (obj->madv == __I915_MADV_PURGED)
  707. i915_gem_object_truncate(obj);
  708. /* and flush dirty cachelines in case the object isn't in the cpu write
  709. * domain anymore. */
  710. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  711. i915_gem_clflush_object(obj);
  712. intel_gtt_chipset_flush();
  713. }
  714. }
  715. if (needs_clflush_after)
  716. intel_gtt_chipset_flush();
  717. return ret;
  718. }
  719. /**
  720. * Writes data to the object referenced by handle.
  721. *
  722. * On error, the contents of the buffer that were to be modified are undefined.
  723. */
  724. int
  725. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  726. struct drm_file *file)
  727. {
  728. struct drm_i915_gem_pwrite *args = data;
  729. struct drm_i915_gem_object *obj;
  730. int ret;
  731. if (args->size == 0)
  732. return 0;
  733. if (!access_ok(VERIFY_READ,
  734. (char __user *)(uintptr_t)args->data_ptr,
  735. args->size))
  736. return -EFAULT;
  737. ret = fault_in_multipages_readable((char __user *)(uintptr_t)args->data_ptr,
  738. args->size);
  739. if (ret)
  740. return -EFAULT;
  741. ret = i915_mutex_lock_interruptible(dev);
  742. if (ret)
  743. return ret;
  744. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  745. if (&obj->base == NULL) {
  746. ret = -ENOENT;
  747. goto unlock;
  748. }
  749. /* Bounds check destination. */
  750. if (args->offset > obj->base.size ||
  751. args->size > obj->base.size - args->offset) {
  752. ret = -EINVAL;
  753. goto out;
  754. }
  755. /* prime objects have no backing filp to GEM pread/pwrite
  756. * pages from.
  757. */
  758. if (!obj->base.filp) {
  759. ret = -EINVAL;
  760. goto out;
  761. }
  762. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  763. ret = -EFAULT;
  764. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  765. * it would end up going through the fenced access, and we'll get
  766. * different detiling behavior between reading and writing.
  767. * pread/pwrite currently are reading and writing from the CPU
  768. * perspective, requiring manual detiling by the client.
  769. */
  770. if (obj->phys_obj) {
  771. ret = i915_gem_phys_pwrite(dev, obj, args, file);
  772. goto out;
  773. }
  774. if (obj->gtt_space &&
  775. obj->cache_level == I915_CACHE_NONE &&
  776. obj->tiling_mode == I915_TILING_NONE &&
  777. obj->map_and_fenceable &&
  778. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  779. ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
  780. /* Note that the gtt paths might fail with non-page-backed user
  781. * pointers (e.g. gtt mappings when moving data between
  782. * textures). Fallback to the shmem path in that case. */
  783. }
  784. if (ret == -EFAULT)
  785. ret = i915_gem_shmem_pwrite(dev, obj, args, file);
  786. out:
  787. drm_gem_object_unreference(&obj->base);
  788. unlock:
  789. mutex_unlock(&dev->struct_mutex);
  790. return ret;
  791. }
  792. /**
  793. * Called when user space prepares to use an object with the CPU, either
  794. * through the mmap ioctl's mapping or a GTT mapping.
  795. */
  796. int
  797. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  798. struct drm_file *file)
  799. {
  800. struct drm_i915_gem_set_domain *args = data;
  801. struct drm_i915_gem_object *obj;
  802. uint32_t read_domains = args->read_domains;
  803. uint32_t write_domain = args->write_domain;
  804. int ret;
  805. /* Only handle setting domains to types used by the CPU. */
  806. if (write_domain & I915_GEM_GPU_DOMAINS)
  807. return -EINVAL;
  808. if (read_domains & I915_GEM_GPU_DOMAINS)
  809. return -EINVAL;
  810. /* Having something in the write domain implies it's in the read
  811. * domain, and only that read domain. Enforce that in the request.
  812. */
  813. if (write_domain != 0 && read_domains != write_domain)
  814. return -EINVAL;
  815. ret = i915_mutex_lock_interruptible(dev);
  816. if (ret)
  817. return ret;
  818. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  819. if (&obj->base == NULL) {
  820. ret = -ENOENT;
  821. goto unlock;
  822. }
  823. if (read_domains & I915_GEM_DOMAIN_GTT) {
  824. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  825. /* Silently promote "you're not bound, there was nothing to do"
  826. * to success, since the client was just asking us to
  827. * make sure everything was done.
  828. */
  829. if (ret == -EINVAL)
  830. ret = 0;
  831. } else {
  832. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  833. }
  834. drm_gem_object_unreference(&obj->base);
  835. unlock:
  836. mutex_unlock(&dev->struct_mutex);
  837. return ret;
  838. }
  839. /**
  840. * Called when user space has done writes to this buffer
  841. */
  842. int
  843. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  844. struct drm_file *file)
  845. {
  846. struct drm_i915_gem_sw_finish *args = data;
  847. struct drm_i915_gem_object *obj;
  848. int ret = 0;
  849. ret = i915_mutex_lock_interruptible(dev);
  850. if (ret)
  851. return ret;
  852. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  853. if (&obj->base == NULL) {
  854. ret = -ENOENT;
  855. goto unlock;
  856. }
  857. /* Pinned buffers may be scanout, so flush the cache */
  858. if (obj->pin_count)
  859. i915_gem_object_flush_cpu_write_domain(obj);
  860. drm_gem_object_unreference(&obj->base);
  861. unlock:
  862. mutex_unlock(&dev->struct_mutex);
  863. return ret;
  864. }
  865. /**
  866. * Maps the contents of an object, returning the address it is mapped
  867. * into.
  868. *
  869. * While the mapping holds a reference on the contents of the object, it doesn't
  870. * imply a ref on the object itself.
  871. */
  872. int
  873. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  874. struct drm_file *file)
  875. {
  876. struct drm_i915_gem_mmap *args = data;
  877. struct drm_gem_object *obj;
  878. unsigned long addr;
  879. obj = drm_gem_object_lookup(dev, file, args->handle);
  880. if (obj == NULL)
  881. return -ENOENT;
  882. /* prime objects have no backing filp to GEM mmap
  883. * pages from.
  884. */
  885. if (!obj->filp) {
  886. drm_gem_object_unreference_unlocked(obj);
  887. return -EINVAL;
  888. }
  889. addr = vm_mmap(obj->filp, 0, args->size,
  890. PROT_READ | PROT_WRITE, MAP_SHARED,
  891. args->offset);
  892. drm_gem_object_unreference_unlocked(obj);
  893. if (IS_ERR((void *)addr))
  894. return addr;
  895. args->addr_ptr = (uint64_t) addr;
  896. return 0;
  897. }
  898. /**
  899. * i915_gem_fault - fault a page into the GTT
  900. * vma: VMA in question
  901. * vmf: fault info
  902. *
  903. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  904. * from userspace. The fault handler takes care of binding the object to
  905. * the GTT (if needed), allocating and programming a fence register (again,
  906. * only if needed based on whether the old reg is still valid or the object
  907. * is tiled) and inserting a new PTE into the faulting process.
  908. *
  909. * Note that the faulting process may involve evicting existing objects
  910. * from the GTT and/or fence registers to make room. So performance may
  911. * suffer if the GTT working set is large or there are few fence registers
  912. * left.
  913. */
  914. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  915. {
  916. struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
  917. struct drm_device *dev = obj->base.dev;
  918. drm_i915_private_t *dev_priv = dev->dev_private;
  919. pgoff_t page_offset;
  920. unsigned long pfn;
  921. int ret = 0;
  922. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  923. /* We don't use vmf->pgoff since that has the fake offset */
  924. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  925. PAGE_SHIFT;
  926. ret = i915_mutex_lock_interruptible(dev);
  927. if (ret)
  928. goto out;
  929. trace_i915_gem_object_fault(obj, page_offset, true, write);
  930. /* Now bind it into the GTT if needed */
  931. if (!obj->map_and_fenceable) {
  932. ret = i915_gem_object_unbind(obj);
  933. if (ret)
  934. goto unlock;
  935. }
  936. if (!obj->gtt_space) {
  937. ret = i915_gem_object_bind_to_gtt(obj, 0, true);
  938. if (ret)
  939. goto unlock;
  940. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  941. if (ret)
  942. goto unlock;
  943. }
  944. if (!obj->has_global_gtt_mapping)
  945. i915_gem_gtt_bind_object(obj, obj->cache_level);
  946. ret = i915_gem_object_get_fence(obj);
  947. if (ret)
  948. goto unlock;
  949. if (i915_gem_object_is_inactive(obj))
  950. list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  951. obj->fault_mappable = true;
  952. pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
  953. page_offset;
  954. /* Finally, remap it using the new GTT offset */
  955. ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
  956. unlock:
  957. mutex_unlock(&dev->struct_mutex);
  958. out:
  959. switch (ret) {
  960. case -EIO:
  961. case -EAGAIN:
  962. /* Give the error handler a chance to run and move the
  963. * objects off the GPU active list. Next time we service the
  964. * fault, we should be able to transition the page into the
  965. * GTT without touching the GPU (and so avoid further
  966. * EIO/EGAIN). If the GPU is wedged, then there is no issue
  967. * with coherency, just lost writes.
  968. */
  969. set_need_resched();
  970. case 0:
  971. case -ERESTARTSYS:
  972. case -EINTR:
  973. return VM_FAULT_NOPAGE;
  974. case -ENOMEM:
  975. return VM_FAULT_OOM;
  976. default:
  977. return VM_FAULT_SIGBUS;
  978. }
  979. }
  980. /**
  981. * i915_gem_release_mmap - remove physical page mappings
  982. * @obj: obj in question
  983. *
  984. * Preserve the reservation of the mmapping with the DRM core code, but
  985. * relinquish ownership of the pages back to the system.
  986. *
  987. * It is vital that we remove the page mapping if we have mapped a tiled
  988. * object through the GTT and then lose the fence register due to
  989. * resource pressure. Similarly if the object has been moved out of the
  990. * aperture, than pages mapped into userspace must be revoked. Removing the
  991. * mapping will then trigger a page fault on the next user access, allowing
  992. * fixup by i915_gem_fault().
  993. */
  994. void
  995. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  996. {
  997. if (!obj->fault_mappable)
  998. return;
  999. if (obj->base.dev->dev_mapping)
  1000. unmap_mapping_range(obj->base.dev->dev_mapping,
  1001. (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
  1002. obj->base.size, 1);
  1003. obj->fault_mappable = false;
  1004. }
  1005. static uint32_t
  1006. i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
  1007. {
  1008. uint32_t gtt_size;
  1009. if (INTEL_INFO(dev)->gen >= 4 ||
  1010. tiling_mode == I915_TILING_NONE)
  1011. return size;
  1012. /* Previous chips need a power-of-two fence region when tiling */
  1013. if (INTEL_INFO(dev)->gen == 3)
  1014. gtt_size = 1024*1024;
  1015. else
  1016. gtt_size = 512*1024;
  1017. while (gtt_size < size)
  1018. gtt_size <<= 1;
  1019. return gtt_size;
  1020. }
  1021. /**
  1022. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1023. * @obj: object to check
  1024. *
  1025. * Return the required GTT alignment for an object, taking into account
  1026. * potential fence register mapping.
  1027. */
  1028. static uint32_t
  1029. i915_gem_get_gtt_alignment(struct drm_device *dev,
  1030. uint32_t size,
  1031. int tiling_mode)
  1032. {
  1033. /*
  1034. * Minimum alignment is 4k (GTT page size), but might be greater
  1035. * if a fence register is needed for the object.
  1036. */
  1037. if (INTEL_INFO(dev)->gen >= 4 ||
  1038. tiling_mode == I915_TILING_NONE)
  1039. return 4096;
  1040. /*
  1041. * Previous chips need to be aligned to the size of the smallest
  1042. * fence register that can contain the object.
  1043. */
  1044. return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1045. }
  1046. /**
  1047. * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
  1048. * unfenced object
  1049. * @dev: the device
  1050. * @size: size of the object
  1051. * @tiling_mode: tiling mode of the object
  1052. *
  1053. * Return the required GTT alignment for an object, only taking into account
  1054. * unfenced tiled surface requirements.
  1055. */
  1056. uint32_t
  1057. i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
  1058. uint32_t size,
  1059. int tiling_mode)
  1060. {
  1061. /*
  1062. * Minimum alignment is 4k (GTT page size) for sane hw.
  1063. */
  1064. if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
  1065. tiling_mode == I915_TILING_NONE)
  1066. return 4096;
  1067. /* Previous hardware however needs to be aligned to a power-of-two
  1068. * tile height. The simplest method for determining this is to reuse
  1069. * the power-of-tile object size.
  1070. */
  1071. return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1072. }
  1073. int
  1074. i915_gem_mmap_gtt(struct drm_file *file,
  1075. struct drm_device *dev,
  1076. uint32_t handle,
  1077. uint64_t *offset)
  1078. {
  1079. struct drm_i915_private *dev_priv = dev->dev_private;
  1080. struct drm_i915_gem_object *obj;
  1081. int ret;
  1082. ret = i915_mutex_lock_interruptible(dev);
  1083. if (ret)
  1084. return ret;
  1085. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  1086. if (&obj->base == NULL) {
  1087. ret = -ENOENT;
  1088. goto unlock;
  1089. }
  1090. if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
  1091. ret = -E2BIG;
  1092. goto out;
  1093. }
  1094. if (obj->madv != I915_MADV_WILLNEED) {
  1095. DRM_ERROR("Attempting to mmap a purgeable buffer\n");
  1096. ret = -EINVAL;
  1097. goto out;
  1098. }
  1099. if (!obj->base.map_list.map) {
  1100. ret = drm_gem_create_mmap_offset(&obj->base);
  1101. if (ret)
  1102. goto out;
  1103. }
  1104. *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
  1105. out:
  1106. drm_gem_object_unreference(&obj->base);
  1107. unlock:
  1108. mutex_unlock(&dev->struct_mutex);
  1109. return ret;
  1110. }
  1111. /**
  1112. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1113. * @dev: DRM device
  1114. * @data: GTT mapping ioctl data
  1115. * @file: GEM object info
  1116. *
  1117. * Simply returns the fake offset to userspace so it can mmap it.
  1118. * The mmap call will end up in drm_gem_mmap(), which will set things
  1119. * up so we can get faults in the handler above.
  1120. *
  1121. * The fault handler will take care of binding the object into the GTT
  1122. * (since it may have been evicted to make room for something), allocating
  1123. * a fence register, and mapping the appropriate aperture address into
  1124. * userspace.
  1125. */
  1126. int
  1127. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1128. struct drm_file *file)
  1129. {
  1130. struct drm_i915_gem_mmap_gtt *args = data;
  1131. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1132. }
  1133. int
  1134. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
  1135. gfp_t gfpmask)
  1136. {
  1137. int page_count, i;
  1138. struct address_space *mapping;
  1139. struct inode *inode;
  1140. struct page *page;
  1141. if (obj->pages || obj->sg_table)
  1142. return 0;
  1143. /* Get the list of pages out of our struct file. They'll be pinned
  1144. * at this point until we release them.
  1145. */
  1146. page_count = obj->base.size / PAGE_SIZE;
  1147. BUG_ON(obj->pages != NULL);
  1148. obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
  1149. if (obj->pages == NULL)
  1150. return -ENOMEM;
  1151. inode = obj->base.filp->f_path.dentry->d_inode;
  1152. mapping = inode->i_mapping;
  1153. gfpmask |= mapping_gfp_mask(mapping);
  1154. for (i = 0; i < page_count; i++) {
  1155. page = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
  1156. if (IS_ERR(page))
  1157. goto err_pages;
  1158. obj->pages[i] = page;
  1159. }
  1160. if (i915_gem_object_needs_bit17_swizzle(obj))
  1161. i915_gem_object_do_bit_17_swizzle(obj);
  1162. return 0;
  1163. err_pages:
  1164. while (i--)
  1165. page_cache_release(obj->pages[i]);
  1166. drm_free_large(obj->pages);
  1167. obj->pages = NULL;
  1168. return PTR_ERR(page);
  1169. }
  1170. static void
  1171. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  1172. {
  1173. int page_count = obj->base.size / PAGE_SIZE;
  1174. int i;
  1175. if (!obj->pages)
  1176. return;
  1177. BUG_ON(obj->madv == __I915_MADV_PURGED);
  1178. if (i915_gem_object_needs_bit17_swizzle(obj))
  1179. i915_gem_object_save_bit_17_swizzle(obj);
  1180. if (obj->madv == I915_MADV_DONTNEED)
  1181. obj->dirty = 0;
  1182. for (i = 0; i < page_count; i++) {
  1183. if (obj->dirty)
  1184. set_page_dirty(obj->pages[i]);
  1185. if (obj->madv == I915_MADV_WILLNEED)
  1186. mark_page_accessed(obj->pages[i]);
  1187. page_cache_release(obj->pages[i]);
  1188. }
  1189. obj->dirty = 0;
  1190. drm_free_large(obj->pages);
  1191. obj->pages = NULL;
  1192. }
  1193. void
  1194. i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
  1195. struct intel_ring_buffer *ring,
  1196. u32 seqno)
  1197. {
  1198. struct drm_device *dev = obj->base.dev;
  1199. struct drm_i915_private *dev_priv = dev->dev_private;
  1200. BUG_ON(ring == NULL);
  1201. obj->ring = ring;
  1202. /* Add a reference if we're newly entering the active list. */
  1203. if (!obj->active) {
  1204. drm_gem_object_reference(&obj->base);
  1205. obj->active = 1;
  1206. }
  1207. /* Move from whatever list we were on to the tail of execution. */
  1208. list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
  1209. list_move_tail(&obj->ring_list, &ring->active_list);
  1210. obj->last_rendering_seqno = seqno;
  1211. if (obj->fenced_gpu_access) {
  1212. obj->last_fenced_seqno = seqno;
  1213. /* Bump MRU to take account of the delayed flush */
  1214. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  1215. struct drm_i915_fence_reg *reg;
  1216. reg = &dev_priv->fence_regs[obj->fence_reg];
  1217. list_move_tail(&reg->lru_list,
  1218. &dev_priv->mm.fence_list);
  1219. }
  1220. }
  1221. }
  1222. static void
  1223. i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
  1224. {
  1225. list_del_init(&obj->ring_list);
  1226. obj->last_rendering_seqno = 0;
  1227. obj->last_fenced_seqno = 0;
  1228. }
  1229. static void
  1230. i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
  1231. {
  1232. struct drm_device *dev = obj->base.dev;
  1233. drm_i915_private_t *dev_priv = dev->dev_private;
  1234. BUG_ON(!obj->active);
  1235. list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
  1236. i915_gem_object_move_off_active(obj);
  1237. }
  1238. static void
  1239. i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
  1240. {
  1241. struct drm_device *dev = obj->base.dev;
  1242. struct drm_i915_private *dev_priv = dev->dev_private;
  1243. list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  1244. BUG_ON(!list_empty(&obj->gpu_write_list));
  1245. BUG_ON(!obj->active);
  1246. obj->ring = NULL;
  1247. i915_gem_object_move_off_active(obj);
  1248. obj->fenced_gpu_access = false;
  1249. obj->active = 0;
  1250. obj->pending_gpu_write = false;
  1251. drm_gem_object_unreference(&obj->base);
  1252. WARN_ON(i915_verify_lists(dev));
  1253. }
  1254. /* Immediately discard the backing storage */
  1255. static void
  1256. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1257. {
  1258. struct inode *inode;
  1259. /* Our goal here is to return as much of the memory as
  1260. * is possible back to the system as we are called from OOM.
  1261. * To do this we must instruct the shmfs to drop all of its
  1262. * backing pages, *now*.
  1263. */
  1264. inode = obj->base.filp->f_path.dentry->d_inode;
  1265. shmem_truncate_range(inode, 0, (loff_t)-1);
  1266. if (obj->base.map_list.map)
  1267. drm_gem_free_mmap_offset(&obj->base);
  1268. obj->madv = __I915_MADV_PURGED;
  1269. }
  1270. static inline int
  1271. i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
  1272. {
  1273. return obj->madv == I915_MADV_DONTNEED;
  1274. }
  1275. static void
  1276. i915_gem_process_flushing_list(struct intel_ring_buffer *ring,
  1277. uint32_t flush_domains)
  1278. {
  1279. struct drm_i915_gem_object *obj, *next;
  1280. list_for_each_entry_safe(obj, next,
  1281. &ring->gpu_write_list,
  1282. gpu_write_list) {
  1283. if (obj->base.write_domain & flush_domains) {
  1284. uint32_t old_write_domain = obj->base.write_domain;
  1285. obj->base.write_domain = 0;
  1286. list_del_init(&obj->gpu_write_list);
  1287. i915_gem_object_move_to_active(obj, ring,
  1288. i915_gem_next_request_seqno(ring));
  1289. trace_i915_gem_object_change_domain(obj,
  1290. obj->base.read_domains,
  1291. old_write_domain);
  1292. }
  1293. }
  1294. }
  1295. static u32
  1296. i915_gem_get_seqno(struct drm_device *dev)
  1297. {
  1298. drm_i915_private_t *dev_priv = dev->dev_private;
  1299. u32 seqno = dev_priv->next_seqno;
  1300. /* reserve 0 for non-seqno */
  1301. if (++dev_priv->next_seqno == 0)
  1302. dev_priv->next_seqno = 1;
  1303. return seqno;
  1304. }
  1305. u32
  1306. i915_gem_next_request_seqno(struct intel_ring_buffer *ring)
  1307. {
  1308. if (ring->outstanding_lazy_request == 0)
  1309. ring->outstanding_lazy_request = i915_gem_get_seqno(ring->dev);
  1310. return ring->outstanding_lazy_request;
  1311. }
  1312. int
  1313. i915_add_request(struct intel_ring_buffer *ring,
  1314. struct drm_file *file,
  1315. struct drm_i915_gem_request *request)
  1316. {
  1317. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  1318. uint32_t seqno;
  1319. u32 request_ring_position;
  1320. int was_empty;
  1321. int ret;
  1322. BUG_ON(request == NULL);
  1323. seqno = i915_gem_next_request_seqno(ring);
  1324. /* Record the position of the start of the request so that
  1325. * should we detect the updated seqno part-way through the
  1326. * GPU processing the request, we never over-estimate the
  1327. * position of the head.
  1328. */
  1329. request_ring_position = intel_ring_get_tail(ring);
  1330. ret = ring->add_request(ring, &seqno);
  1331. if (ret)
  1332. return ret;
  1333. trace_i915_gem_request_add(ring, seqno);
  1334. request->seqno = seqno;
  1335. request->ring = ring;
  1336. request->tail = request_ring_position;
  1337. request->emitted_jiffies = jiffies;
  1338. was_empty = list_empty(&ring->request_list);
  1339. list_add_tail(&request->list, &ring->request_list);
  1340. if (file) {
  1341. struct drm_i915_file_private *file_priv = file->driver_priv;
  1342. spin_lock(&file_priv->mm.lock);
  1343. request->file_priv = file_priv;
  1344. list_add_tail(&request->client_list,
  1345. &file_priv->mm.request_list);
  1346. spin_unlock(&file_priv->mm.lock);
  1347. }
  1348. ring->outstanding_lazy_request = 0;
  1349. if (!dev_priv->mm.suspended) {
  1350. if (i915_enable_hangcheck) {
  1351. mod_timer(&dev_priv->hangcheck_timer,
  1352. jiffies +
  1353. msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
  1354. }
  1355. if (was_empty)
  1356. queue_delayed_work(dev_priv->wq,
  1357. &dev_priv->mm.retire_work, HZ);
  1358. }
  1359. return 0;
  1360. }
  1361. static inline void
  1362. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  1363. {
  1364. struct drm_i915_file_private *file_priv = request->file_priv;
  1365. if (!file_priv)
  1366. return;
  1367. spin_lock(&file_priv->mm.lock);
  1368. if (request->file_priv) {
  1369. list_del(&request->client_list);
  1370. request->file_priv = NULL;
  1371. }
  1372. spin_unlock(&file_priv->mm.lock);
  1373. }
  1374. static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
  1375. struct intel_ring_buffer *ring)
  1376. {
  1377. while (!list_empty(&ring->request_list)) {
  1378. struct drm_i915_gem_request *request;
  1379. request = list_first_entry(&ring->request_list,
  1380. struct drm_i915_gem_request,
  1381. list);
  1382. list_del(&request->list);
  1383. i915_gem_request_remove_from_client(request);
  1384. kfree(request);
  1385. }
  1386. while (!list_empty(&ring->active_list)) {
  1387. struct drm_i915_gem_object *obj;
  1388. obj = list_first_entry(&ring->active_list,
  1389. struct drm_i915_gem_object,
  1390. ring_list);
  1391. obj->base.write_domain = 0;
  1392. list_del_init(&obj->gpu_write_list);
  1393. i915_gem_object_move_to_inactive(obj);
  1394. }
  1395. }
  1396. static void i915_gem_reset_fences(struct drm_device *dev)
  1397. {
  1398. struct drm_i915_private *dev_priv = dev->dev_private;
  1399. int i;
  1400. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  1401. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  1402. i915_gem_write_fence(dev, i, NULL);
  1403. if (reg->obj)
  1404. i915_gem_object_fence_lost(reg->obj);
  1405. reg->pin_count = 0;
  1406. reg->obj = NULL;
  1407. INIT_LIST_HEAD(&reg->lru_list);
  1408. }
  1409. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  1410. }
  1411. void i915_gem_reset(struct drm_device *dev)
  1412. {
  1413. struct drm_i915_private *dev_priv = dev->dev_private;
  1414. struct drm_i915_gem_object *obj;
  1415. struct intel_ring_buffer *ring;
  1416. int i;
  1417. for_each_ring(ring, dev_priv, i)
  1418. i915_gem_reset_ring_lists(dev_priv, ring);
  1419. /* Remove anything from the flushing lists. The GPU cache is likely
  1420. * to be lost on reset along with the data, so simply move the
  1421. * lost bo to the inactive list.
  1422. */
  1423. while (!list_empty(&dev_priv->mm.flushing_list)) {
  1424. obj = list_first_entry(&dev_priv->mm.flushing_list,
  1425. struct drm_i915_gem_object,
  1426. mm_list);
  1427. obj->base.write_domain = 0;
  1428. list_del_init(&obj->gpu_write_list);
  1429. i915_gem_object_move_to_inactive(obj);
  1430. }
  1431. /* Move everything out of the GPU domains to ensure we do any
  1432. * necessary invalidation upon reuse.
  1433. */
  1434. list_for_each_entry(obj,
  1435. &dev_priv->mm.inactive_list,
  1436. mm_list)
  1437. {
  1438. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  1439. }
  1440. /* The fence registers are invalidated so clear them out */
  1441. i915_gem_reset_fences(dev);
  1442. }
  1443. /**
  1444. * This function clears the request list as sequence numbers are passed.
  1445. */
  1446. void
  1447. i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
  1448. {
  1449. uint32_t seqno;
  1450. int i;
  1451. if (list_empty(&ring->request_list))
  1452. return;
  1453. WARN_ON(i915_verify_lists(ring->dev));
  1454. seqno = ring->get_seqno(ring);
  1455. for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
  1456. if (seqno >= ring->sync_seqno[i])
  1457. ring->sync_seqno[i] = 0;
  1458. while (!list_empty(&ring->request_list)) {
  1459. struct drm_i915_gem_request *request;
  1460. request = list_first_entry(&ring->request_list,
  1461. struct drm_i915_gem_request,
  1462. list);
  1463. if (!i915_seqno_passed(seqno, request->seqno))
  1464. break;
  1465. trace_i915_gem_request_retire(ring, request->seqno);
  1466. /* We know the GPU must have read the request to have
  1467. * sent us the seqno + interrupt, so use the position
  1468. * of tail of the request to update the last known position
  1469. * of the GPU head.
  1470. */
  1471. ring->last_retired_head = request->tail;
  1472. list_del(&request->list);
  1473. i915_gem_request_remove_from_client(request);
  1474. kfree(request);
  1475. }
  1476. /* Move any buffers on the active list that are no longer referenced
  1477. * by the ringbuffer to the flushing/inactive lists as appropriate.
  1478. */
  1479. while (!list_empty(&ring->active_list)) {
  1480. struct drm_i915_gem_object *obj;
  1481. obj = list_first_entry(&ring->active_list,
  1482. struct drm_i915_gem_object,
  1483. ring_list);
  1484. if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
  1485. break;
  1486. if (obj->base.write_domain != 0)
  1487. i915_gem_object_move_to_flushing(obj);
  1488. else
  1489. i915_gem_object_move_to_inactive(obj);
  1490. }
  1491. if (unlikely(ring->trace_irq_seqno &&
  1492. i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
  1493. ring->irq_put(ring);
  1494. ring->trace_irq_seqno = 0;
  1495. }
  1496. WARN_ON(i915_verify_lists(ring->dev));
  1497. }
  1498. void
  1499. i915_gem_retire_requests(struct drm_device *dev)
  1500. {
  1501. drm_i915_private_t *dev_priv = dev->dev_private;
  1502. struct intel_ring_buffer *ring;
  1503. int i;
  1504. for_each_ring(ring, dev_priv, i)
  1505. i915_gem_retire_requests_ring(ring);
  1506. }
  1507. static void
  1508. i915_gem_retire_work_handler(struct work_struct *work)
  1509. {
  1510. drm_i915_private_t *dev_priv;
  1511. struct drm_device *dev;
  1512. struct intel_ring_buffer *ring;
  1513. bool idle;
  1514. int i;
  1515. dev_priv = container_of(work, drm_i915_private_t,
  1516. mm.retire_work.work);
  1517. dev = dev_priv->dev;
  1518. /* Come back later if the device is busy... */
  1519. if (!mutex_trylock(&dev->struct_mutex)) {
  1520. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
  1521. return;
  1522. }
  1523. i915_gem_retire_requests(dev);
  1524. /* Send a periodic flush down the ring so we don't hold onto GEM
  1525. * objects indefinitely.
  1526. */
  1527. idle = true;
  1528. for_each_ring(ring, dev_priv, i) {
  1529. if (!list_empty(&ring->gpu_write_list)) {
  1530. struct drm_i915_gem_request *request;
  1531. int ret;
  1532. ret = i915_gem_flush_ring(ring,
  1533. 0, I915_GEM_GPU_DOMAINS);
  1534. request = kzalloc(sizeof(*request), GFP_KERNEL);
  1535. if (ret || request == NULL ||
  1536. i915_add_request(ring, NULL, request))
  1537. kfree(request);
  1538. }
  1539. idle &= list_empty(&ring->request_list);
  1540. }
  1541. if (!dev_priv->mm.suspended && !idle)
  1542. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
  1543. mutex_unlock(&dev->struct_mutex);
  1544. }
  1545. static int
  1546. i915_gem_check_wedge(struct drm_i915_private *dev_priv)
  1547. {
  1548. BUG_ON(!mutex_is_locked(&dev_priv->dev->struct_mutex));
  1549. if (atomic_read(&dev_priv->mm.wedged)) {
  1550. struct completion *x = &dev_priv->error_completion;
  1551. bool recovery_complete;
  1552. unsigned long flags;
  1553. /* Give the error handler a chance to run. */
  1554. spin_lock_irqsave(&x->wait.lock, flags);
  1555. recovery_complete = x->done > 0;
  1556. spin_unlock_irqrestore(&x->wait.lock, flags);
  1557. return recovery_complete ? -EIO : -EAGAIN;
  1558. }
  1559. return 0;
  1560. }
  1561. /*
  1562. * Compare seqno against outstanding lazy request. Emit a request if they are
  1563. * equal.
  1564. */
  1565. static int
  1566. i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno)
  1567. {
  1568. int ret = 0;
  1569. BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex));
  1570. if (seqno == ring->outstanding_lazy_request) {
  1571. struct drm_i915_gem_request *request;
  1572. request = kzalloc(sizeof(*request), GFP_KERNEL);
  1573. if (request == NULL)
  1574. return -ENOMEM;
  1575. ret = i915_add_request(ring, NULL, request);
  1576. if (ret) {
  1577. kfree(request);
  1578. return ret;
  1579. }
  1580. BUG_ON(seqno != request->seqno);
  1581. }
  1582. return ret;
  1583. }
  1584. static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno,
  1585. bool interruptible)
  1586. {
  1587. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  1588. int ret = 0;
  1589. if (i915_seqno_passed(ring->get_seqno(ring), seqno))
  1590. return 0;
  1591. trace_i915_gem_request_wait_begin(ring, seqno);
  1592. if (WARN_ON(!ring->irq_get(ring)))
  1593. return -ENODEV;
  1594. #define EXIT_COND \
  1595. (i915_seqno_passed(ring->get_seqno(ring), seqno) || \
  1596. atomic_read(&dev_priv->mm.wedged))
  1597. if (interruptible)
  1598. ret = wait_event_interruptible(ring->irq_queue,
  1599. EXIT_COND);
  1600. else
  1601. wait_event(ring->irq_queue, EXIT_COND);
  1602. ring->irq_put(ring);
  1603. trace_i915_gem_request_wait_end(ring, seqno);
  1604. #undef EXIT_COND
  1605. return ret;
  1606. }
  1607. /**
  1608. * Waits for a sequence number to be signaled, and cleans up the
  1609. * request and object lists appropriately for that event.
  1610. */
  1611. int
  1612. i915_wait_request(struct intel_ring_buffer *ring,
  1613. uint32_t seqno)
  1614. {
  1615. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  1616. int ret = 0;
  1617. BUG_ON(seqno == 0);
  1618. ret = i915_gem_check_wedge(dev_priv);
  1619. if (ret)
  1620. return ret;
  1621. ret = i915_gem_check_olr(ring, seqno);
  1622. if (ret)
  1623. return ret;
  1624. ret = __wait_seqno(ring, seqno, dev_priv->mm.interruptible);
  1625. if (atomic_read(&dev_priv->mm.wedged))
  1626. ret = -EAGAIN;
  1627. return ret;
  1628. }
  1629. /**
  1630. * Ensures that all rendering to the object has completed and the object is
  1631. * safe to unbind from the GTT or access from the CPU.
  1632. */
  1633. int
  1634. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj)
  1635. {
  1636. int ret;
  1637. /* This function only exists to support waiting for existing rendering,
  1638. * not for emitting required flushes.
  1639. */
  1640. BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
  1641. /* If there is rendering queued on the buffer being evicted, wait for
  1642. * it.
  1643. */
  1644. if (obj->active) {
  1645. ret = i915_wait_request(obj->ring, obj->last_rendering_seqno);
  1646. if (ret)
  1647. return ret;
  1648. i915_gem_retire_requests_ring(obj->ring);
  1649. }
  1650. return 0;
  1651. }
  1652. /**
  1653. * i915_gem_object_sync - sync an object to a ring.
  1654. *
  1655. * @obj: object which may be in use on another ring.
  1656. * @to: ring we wish to use the object on. May be NULL.
  1657. *
  1658. * This code is meant to abstract object synchronization with the GPU.
  1659. * Calling with NULL implies synchronizing the object with the CPU
  1660. * rather than a particular GPU ring.
  1661. *
  1662. * Returns 0 if successful, else propagates up the lower layer error.
  1663. */
  1664. int
  1665. i915_gem_object_sync(struct drm_i915_gem_object *obj,
  1666. struct intel_ring_buffer *to)
  1667. {
  1668. struct intel_ring_buffer *from = obj->ring;
  1669. u32 seqno;
  1670. int ret, idx;
  1671. if (from == NULL || to == from)
  1672. return 0;
  1673. if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
  1674. return i915_gem_object_wait_rendering(obj);
  1675. idx = intel_ring_sync_index(from, to);
  1676. seqno = obj->last_rendering_seqno;
  1677. if (seqno <= from->sync_seqno[idx])
  1678. return 0;
  1679. ret = i915_gem_check_olr(obj->ring, seqno);
  1680. if (ret)
  1681. return ret;
  1682. ret = to->sync_to(to, from, seqno);
  1683. if (!ret)
  1684. from->sync_seqno[idx] = seqno;
  1685. return ret;
  1686. }
  1687. static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
  1688. {
  1689. u32 old_write_domain, old_read_domains;
  1690. /* Act a barrier for all accesses through the GTT */
  1691. mb();
  1692. /* Force a pagefault for domain tracking on next user access */
  1693. i915_gem_release_mmap(obj);
  1694. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  1695. return;
  1696. old_read_domains = obj->base.read_domains;
  1697. old_write_domain = obj->base.write_domain;
  1698. obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
  1699. obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
  1700. trace_i915_gem_object_change_domain(obj,
  1701. old_read_domains,
  1702. old_write_domain);
  1703. }
  1704. /**
  1705. * Unbinds an object from the GTT aperture.
  1706. */
  1707. int
  1708. i915_gem_object_unbind(struct drm_i915_gem_object *obj)
  1709. {
  1710. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  1711. int ret = 0;
  1712. if (obj->gtt_space == NULL)
  1713. return 0;
  1714. if (obj->pin_count != 0) {
  1715. DRM_ERROR("Attempting to unbind pinned buffer\n");
  1716. return -EINVAL;
  1717. }
  1718. ret = i915_gem_object_finish_gpu(obj);
  1719. if (ret)
  1720. return ret;
  1721. /* Continue on if we fail due to EIO, the GPU is hung so we
  1722. * should be safe and we need to cleanup or else we might
  1723. * cause memory corruption through use-after-free.
  1724. */
  1725. i915_gem_object_finish_gtt(obj);
  1726. /* Move the object to the CPU domain to ensure that
  1727. * any possible CPU writes while it's not in the GTT
  1728. * are flushed when we go to remap it.
  1729. */
  1730. if (ret == 0)
  1731. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  1732. if (ret == -ERESTARTSYS)
  1733. return ret;
  1734. if (ret) {
  1735. /* In the event of a disaster, abandon all caches and
  1736. * hope for the best.
  1737. */
  1738. i915_gem_clflush_object(obj);
  1739. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  1740. }
  1741. /* release the fence reg _after_ flushing */
  1742. ret = i915_gem_object_put_fence(obj);
  1743. if (ret)
  1744. return ret;
  1745. trace_i915_gem_object_unbind(obj);
  1746. if (obj->has_global_gtt_mapping)
  1747. i915_gem_gtt_unbind_object(obj);
  1748. if (obj->has_aliasing_ppgtt_mapping) {
  1749. i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
  1750. obj->has_aliasing_ppgtt_mapping = 0;
  1751. }
  1752. i915_gem_gtt_finish_object(obj);
  1753. i915_gem_object_put_pages_gtt(obj);
  1754. list_del_init(&obj->gtt_list);
  1755. list_del_init(&obj->mm_list);
  1756. /* Avoid an unnecessary call to unbind on rebind. */
  1757. obj->map_and_fenceable = true;
  1758. drm_mm_put_block(obj->gtt_space);
  1759. obj->gtt_space = NULL;
  1760. obj->gtt_offset = 0;
  1761. if (i915_gem_object_is_purgeable(obj))
  1762. i915_gem_object_truncate(obj);
  1763. return ret;
  1764. }
  1765. int
  1766. i915_gem_flush_ring(struct intel_ring_buffer *ring,
  1767. uint32_t invalidate_domains,
  1768. uint32_t flush_domains)
  1769. {
  1770. int ret;
  1771. if (((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) == 0)
  1772. return 0;
  1773. trace_i915_gem_ring_flush(ring, invalidate_domains, flush_domains);
  1774. ret = ring->flush(ring, invalidate_domains, flush_domains);
  1775. if (ret)
  1776. return ret;
  1777. if (flush_domains & I915_GEM_GPU_DOMAINS)
  1778. i915_gem_process_flushing_list(ring, flush_domains);
  1779. return 0;
  1780. }
  1781. static int i915_ring_idle(struct intel_ring_buffer *ring)
  1782. {
  1783. int ret;
  1784. if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
  1785. return 0;
  1786. if (!list_empty(&ring->gpu_write_list)) {
  1787. ret = i915_gem_flush_ring(ring,
  1788. I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
  1789. if (ret)
  1790. return ret;
  1791. }
  1792. return i915_wait_request(ring, i915_gem_next_request_seqno(ring));
  1793. }
  1794. int i915_gpu_idle(struct drm_device *dev)
  1795. {
  1796. drm_i915_private_t *dev_priv = dev->dev_private;
  1797. struct intel_ring_buffer *ring;
  1798. int ret, i;
  1799. /* Flush everything onto the inactive list. */
  1800. for_each_ring(ring, dev_priv, i) {
  1801. ret = i915_ring_idle(ring);
  1802. if (ret)
  1803. return ret;
  1804. /* Is the device fubar? */
  1805. if (WARN_ON(!list_empty(&ring->gpu_write_list)))
  1806. return -EBUSY;
  1807. }
  1808. return 0;
  1809. }
  1810. static void sandybridge_write_fence_reg(struct drm_device *dev, int reg,
  1811. struct drm_i915_gem_object *obj)
  1812. {
  1813. drm_i915_private_t *dev_priv = dev->dev_private;
  1814. uint64_t val;
  1815. if (obj) {
  1816. u32 size = obj->gtt_space->size;
  1817. val = (uint64_t)((obj->gtt_offset + size - 4096) &
  1818. 0xfffff000) << 32;
  1819. val |= obj->gtt_offset & 0xfffff000;
  1820. val |= (uint64_t)((obj->stride / 128) - 1) <<
  1821. SANDYBRIDGE_FENCE_PITCH_SHIFT;
  1822. if (obj->tiling_mode == I915_TILING_Y)
  1823. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  1824. val |= I965_FENCE_REG_VALID;
  1825. } else
  1826. val = 0;
  1827. I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + reg * 8, val);
  1828. POSTING_READ(FENCE_REG_SANDYBRIDGE_0 + reg * 8);
  1829. }
  1830. static void i965_write_fence_reg(struct drm_device *dev, int reg,
  1831. struct drm_i915_gem_object *obj)
  1832. {
  1833. drm_i915_private_t *dev_priv = dev->dev_private;
  1834. uint64_t val;
  1835. if (obj) {
  1836. u32 size = obj->gtt_space->size;
  1837. val = (uint64_t)((obj->gtt_offset + size - 4096) &
  1838. 0xfffff000) << 32;
  1839. val |= obj->gtt_offset & 0xfffff000;
  1840. val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
  1841. if (obj->tiling_mode == I915_TILING_Y)
  1842. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  1843. val |= I965_FENCE_REG_VALID;
  1844. } else
  1845. val = 0;
  1846. I915_WRITE64(FENCE_REG_965_0 + reg * 8, val);
  1847. POSTING_READ(FENCE_REG_965_0 + reg * 8);
  1848. }
  1849. static void i915_write_fence_reg(struct drm_device *dev, int reg,
  1850. struct drm_i915_gem_object *obj)
  1851. {
  1852. drm_i915_private_t *dev_priv = dev->dev_private;
  1853. u32 val;
  1854. if (obj) {
  1855. u32 size = obj->gtt_space->size;
  1856. int pitch_val;
  1857. int tile_width;
  1858. WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
  1859. (size & -size) != size ||
  1860. (obj->gtt_offset & (size - 1)),
  1861. "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
  1862. obj->gtt_offset, obj->map_and_fenceable, size);
  1863. if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
  1864. tile_width = 128;
  1865. else
  1866. tile_width = 512;
  1867. /* Note: pitch better be a power of two tile widths */
  1868. pitch_val = obj->stride / tile_width;
  1869. pitch_val = ffs(pitch_val) - 1;
  1870. val = obj->gtt_offset;
  1871. if (obj->tiling_mode == I915_TILING_Y)
  1872. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  1873. val |= I915_FENCE_SIZE_BITS(size);
  1874. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  1875. val |= I830_FENCE_REG_VALID;
  1876. } else
  1877. val = 0;
  1878. if (reg < 8)
  1879. reg = FENCE_REG_830_0 + reg * 4;
  1880. else
  1881. reg = FENCE_REG_945_8 + (reg - 8) * 4;
  1882. I915_WRITE(reg, val);
  1883. POSTING_READ(reg);
  1884. }
  1885. static void i830_write_fence_reg(struct drm_device *dev, int reg,
  1886. struct drm_i915_gem_object *obj)
  1887. {
  1888. drm_i915_private_t *dev_priv = dev->dev_private;
  1889. uint32_t val;
  1890. if (obj) {
  1891. u32 size = obj->gtt_space->size;
  1892. uint32_t pitch_val;
  1893. WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
  1894. (size & -size) != size ||
  1895. (obj->gtt_offset & (size - 1)),
  1896. "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
  1897. obj->gtt_offset, size);
  1898. pitch_val = obj->stride / 128;
  1899. pitch_val = ffs(pitch_val) - 1;
  1900. val = obj->gtt_offset;
  1901. if (obj->tiling_mode == I915_TILING_Y)
  1902. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  1903. val |= I830_FENCE_SIZE_BITS(size);
  1904. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  1905. val |= I830_FENCE_REG_VALID;
  1906. } else
  1907. val = 0;
  1908. I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
  1909. POSTING_READ(FENCE_REG_830_0 + reg * 4);
  1910. }
  1911. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  1912. struct drm_i915_gem_object *obj)
  1913. {
  1914. switch (INTEL_INFO(dev)->gen) {
  1915. case 7:
  1916. case 6: sandybridge_write_fence_reg(dev, reg, obj); break;
  1917. case 5:
  1918. case 4: i965_write_fence_reg(dev, reg, obj); break;
  1919. case 3: i915_write_fence_reg(dev, reg, obj); break;
  1920. case 2: i830_write_fence_reg(dev, reg, obj); break;
  1921. default: break;
  1922. }
  1923. }
  1924. static inline int fence_number(struct drm_i915_private *dev_priv,
  1925. struct drm_i915_fence_reg *fence)
  1926. {
  1927. return fence - dev_priv->fence_regs;
  1928. }
  1929. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  1930. struct drm_i915_fence_reg *fence,
  1931. bool enable)
  1932. {
  1933. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1934. int reg = fence_number(dev_priv, fence);
  1935. i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
  1936. if (enable) {
  1937. obj->fence_reg = reg;
  1938. fence->obj = obj;
  1939. list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
  1940. } else {
  1941. obj->fence_reg = I915_FENCE_REG_NONE;
  1942. fence->obj = NULL;
  1943. list_del_init(&fence->lru_list);
  1944. }
  1945. }
  1946. static int
  1947. i915_gem_object_flush_fence(struct drm_i915_gem_object *obj)
  1948. {
  1949. int ret;
  1950. if (obj->fenced_gpu_access) {
  1951. if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
  1952. ret = i915_gem_flush_ring(obj->ring,
  1953. 0, obj->base.write_domain);
  1954. if (ret)
  1955. return ret;
  1956. }
  1957. obj->fenced_gpu_access = false;
  1958. }
  1959. if (obj->last_fenced_seqno) {
  1960. ret = i915_wait_request(obj->ring, obj->last_fenced_seqno);
  1961. if (ret)
  1962. return ret;
  1963. obj->last_fenced_seqno = 0;
  1964. }
  1965. /* Ensure that all CPU reads are completed before installing a fence
  1966. * and all writes before removing the fence.
  1967. */
  1968. if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
  1969. mb();
  1970. return 0;
  1971. }
  1972. int
  1973. i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
  1974. {
  1975. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1976. int ret;
  1977. ret = i915_gem_object_flush_fence(obj);
  1978. if (ret)
  1979. return ret;
  1980. if (obj->fence_reg == I915_FENCE_REG_NONE)
  1981. return 0;
  1982. i915_gem_object_update_fence(obj,
  1983. &dev_priv->fence_regs[obj->fence_reg],
  1984. false);
  1985. i915_gem_object_fence_lost(obj);
  1986. return 0;
  1987. }
  1988. static struct drm_i915_fence_reg *
  1989. i915_find_fence_reg(struct drm_device *dev)
  1990. {
  1991. struct drm_i915_private *dev_priv = dev->dev_private;
  1992. struct drm_i915_fence_reg *reg, *avail;
  1993. int i;
  1994. /* First try to find a free reg */
  1995. avail = NULL;
  1996. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  1997. reg = &dev_priv->fence_regs[i];
  1998. if (!reg->obj)
  1999. return reg;
  2000. if (!reg->pin_count)
  2001. avail = reg;
  2002. }
  2003. if (avail == NULL)
  2004. return NULL;
  2005. /* None available, try to steal one or wait for a user to finish */
  2006. list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
  2007. if (reg->pin_count)
  2008. continue;
  2009. return reg;
  2010. }
  2011. return NULL;
  2012. }
  2013. /**
  2014. * i915_gem_object_get_fence - set up fencing for an object
  2015. * @obj: object to map through a fence reg
  2016. *
  2017. * When mapping objects through the GTT, userspace wants to be able to write
  2018. * to them without having to worry about swizzling if the object is tiled.
  2019. * This function walks the fence regs looking for a free one for @obj,
  2020. * stealing one if it can't find any.
  2021. *
  2022. * It then sets up the reg based on the object's properties: address, pitch
  2023. * and tiling format.
  2024. *
  2025. * For an untiled surface, this removes any existing fence.
  2026. */
  2027. int
  2028. i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
  2029. {
  2030. struct drm_device *dev = obj->base.dev;
  2031. struct drm_i915_private *dev_priv = dev->dev_private;
  2032. bool enable = obj->tiling_mode != I915_TILING_NONE;
  2033. struct drm_i915_fence_reg *reg;
  2034. int ret;
  2035. /* Have we updated the tiling parameters upon the object and so
  2036. * will need to serialise the write to the associated fence register?
  2037. */
  2038. if (obj->fence_dirty) {
  2039. ret = i915_gem_object_flush_fence(obj);
  2040. if (ret)
  2041. return ret;
  2042. }
  2043. /* Just update our place in the LRU if our fence is getting reused. */
  2044. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  2045. reg = &dev_priv->fence_regs[obj->fence_reg];
  2046. if (!obj->fence_dirty) {
  2047. list_move_tail(&reg->lru_list,
  2048. &dev_priv->mm.fence_list);
  2049. return 0;
  2050. }
  2051. } else if (enable) {
  2052. reg = i915_find_fence_reg(dev);
  2053. if (reg == NULL)
  2054. return -EDEADLK;
  2055. if (reg->obj) {
  2056. struct drm_i915_gem_object *old = reg->obj;
  2057. ret = i915_gem_object_flush_fence(old);
  2058. if (ret)
  2059. return ret;
  2060. i915_gem_object_fence_lost(old);
  2061. }
  2062. } else
  2063. return 0;
  2064. i915_gem_object_update_fence(obj, reg, enable);
  2065. obj->fence_dirty = false;
  2066. return 0;
  2067. }
  2068. /**
  2069. * Finds free space in the GTT aperture and binds the object there.
  2070. */
  2071. static int
  2072. i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
  2073. unsigned alignment,
  2074. bool map_and_fenceable)
  2075. {
  2076. struct drm_device *dev = obj->base.dev;
  2077. drm_i915_private_t *dev_priv = dev->dev_private;
  2078. struct drm_mm_node *free_space;
  2079. gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
  2080. u32 size, fence_size, fence_alignment, unfenced_alignment;
  2081. bool mappable, fenceable;
  2082. int ret;
  2083. if (obj->madv != I915_MADV_WILLNEED) {
  2084. DRM_ERROR("Attempting to bind a purgeable object\n");
  2085. return -EINVAL;
  2086. }
  2087. fence_size = i915_gem_get_gtt_size(dev,
  2088. obj->base.size,
  2089. obj->tiling_mode);
  2090. fence_alignment = i915_gem_get_gtt_alignment(dev,
  2091. obj->base.size,
  2092. obj->tiling_mode);
  2093. unfenced_alignment =
  2094. i915_gem_get_unfenced_gtt_alignment(dev,
  2095. obj->base.size,
  2096. obj->tiling_mode);
  2097. if (alignment == 0)
  2098. alignment = map_and_fenceable ? fence_alignment :
  2099. unfenced_alignment;
  2100. if (map_and_fenceable && alignment & (fence_alignment - 1)) {
  2101. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  2102. return -EINVAL;
  2103. }
  2104. size = map_and_fenceable ? fence_size : obj->base.size;
  2105. /* If the object is bigger than the entire aperture, reject it early
  2106. * before evicting everything in a vain attempt to find space.
  2107. */
  2108. if (obj->base.size >
  2109. (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
  2110. DRM_ERROR("Attempting to bind an object larger than the aperture\n");
  2111. return -E2BIG;
  2112. }
  2113. search_free:
  2114. if (map_and_fenceable)
  2115. free_space =
  2116. drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
  2117. size, alignment, 0,
  2118. dev_priv->mm.gtt_mappable_end,
  2119. 0);
  2120. else
  2121. free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
  2122. size, alignment, 0);
  2123. if (free_space != NULL) {
  2124. if (map_and_fenceable)
  2125. obj->gtt_space =
  2126. drm_mm_get_block_range_generic(free_space,
  2127. size, alignment, 0,
  2128. dev_priv->mm.gtt_mappable_end,
  2129. 0);
  2130. else
  2131. obj->gtt_space =
  2132. drm_mm_get_block(free_space, size, alignment);
  2133. }
  2134. if (obj->gtt_space == NULL) {
  2135. /* If the gtt is empty and we're still having trouble
  2136. * fitting our object in, we're out of memory.
  2137. */
  2138. ret = i915_gem_evict_something(dev, size, alignment,
  2139. map_and_fenceable);
  2140. if (ret)
  2141. return ret;
  2142. goto search_free;
  2143. }
  2144. ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
  2145. if (ret) {
  2146. drm_mm_put_block(obj->gtt_space);
  2147. obj->gtt_space = NULL;
  2148. if (ret == -ENOMEM) {
  2149. /* first try to reclaim some memory by clearing the GTT */
  2150. ret = i915_gem_evict_everything(dev, false);
  2151. if (ret) {
  2152. /* now try to shrink everyone else */
  2153. if (gfpmask) {
  2154. gfpmask = 0;
  2155. goto search_free;
  2156. }
  2157. return -ENOMEM;
  2158. }
  2159. goto search_free;
  2160. }
  2161. return ret;
  2162. }
  2163. ret = i915_gem_gtt_prepare_object(obj);
  2164. if (ret) {
  2165. i915_gem_object_put_pages_gtt(obj);
  2166. drm_mm_put_block(obj->gtt_space);
  2167. obj->gtt_space = NULL;
  2168. if (i915_gem_evict_everything(dev, false))
  2169. return ret;
  2170. goto search_free;
  2171. }
  2172. if (!dev_priv->mm.aliasing_ppgtt)
  2173. i915_gem_gtt_bind_object(obj, obj->cache_level);
  2174. list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
  2175. list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  2176. /* Assert that the object is not currently in any GPU domain. As it
  2177. * wasn't in the GTT, there shouldn't be any way it could have been in
  2178. * a GPU cache
  2179. */
  2180. BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  2181. BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  2182. obj->gtt_offset = obj->gtt_space->start;
  2183. fenceable =
  2184. obj->gtt_space->size == fence_size &&
  2185. (obj->gtt_space->start & (fence_alignment - 1)) == 0;
  2186. mappable =
  2187. obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
  2188. obj->map_and_fenceable = mappable && fenceable;
  2189. trace_i915_gem_object_bind(obj, map_and_fenceable);
  2190. return 0;
  2191. }
  2192. void
  2193. i915_gem_clflush_object(struct drm_i915_gem_object *obj)
  2194. {
  2195. /* If we don't have a page list set up, then we're not pinned
  2196. * to GPU, and we can ignore the cache flush because it'll happen
  2197. * again at bind time.
  2198. */
  2199. if (obj->pages == NULL)
  2200. return;
  2201. /* If the GPU is snooping the contents of the CPU cache,
  2202. * we do not need to manually clear the CPU cache lines. However,
  2203. * the caches are only snooped when the render cache is
  2204. * flushed/invalidated. As we always have to emit invalidations
  2205. * and flushes when moving into and out of the RENDER domain, correct
  2206. * snooping behaviour occurs naturally as the result of our domain
  2207. * tracking.
  2208. */
  2209. if (obj->cache_level != I915_CACHE_NONE)
  2210. return;
  2211. trace_i915_gem_object_clflush(obj);
  2212. drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
  2213. }
  2214. /** Flushes any GPU write domain for the object if it's dirty. */
  2215. static int
  2216. i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj)
  2217. {
  2218. if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
  2219. return 0;
  2220. /* Queue the GPU write cache flushing we need. */
  2221. return i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
  2222. }
  2223. /** Flushes the GTT write domain for the object if it's dirty. */
  2224. static void
  2225. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  2226. {
  2227. uint32_t old_write_domain;
  2228. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  2229. return;
  2230. /* No actual flushing is required for the GTT write domain. Writes
  2231. * to it immediately go to main memory as far as we know, so there's
  2232. * no chipset flush. It also doesn't land in render cache.
  2233. *
  2234. * However, we do have to enforce the order so that all writes through
  2235. * the GTT land before any writes to the device, such as updates to
  2236. * the GATT itself.
  2237. */
  2238. wmb();
  2239. old_write_domain = obj->base.write_domain;
  2240. obj->base.write_domain = 0;
  2241. trace_i915_gem_object_change_domain(obj,
  2242. obj->base.read_domains,
  2243. old_write_domain);
  2244. }
  2245. /** Flushes the CPU write domain for the object if it's dirty. */
  2246. static void
  2247. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  2248. {
  2249. uint32_t old_write_domain;
  2250. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  2251. return;
  2252. i915_gem_clflush_object(obj);
  2253. intel_gtt_chipset_flush();
  2254. old_write_domain = obj->base.write_domain;
  2255. obj->base.write_domain = 0;
  2256. trace_i915_gem_object_change_domain(obj,
  2257. obj->base.read_domains,
  2258. old_write_domain);
  2259. }
  2260. /**
  2261. * Moves a single object to the GTT read, and possibly write domain.
  2262. *
  2263. * This function returns when the move is complete, including waiting on
  2264. * flushes to occur.
  2265. */
  2266. int
  2267. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  2268. {
  2269. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  2270. uint32_t old_write_domain, old_read_domains;
  2271. int ret;
  2272. /* Not valid to be called on unbound objects. */
  2273. if (obj->gtt_space == NULL)
  2274. return -EINVAL;
  2275. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  2276. return 0;
  2277. ret = i915_gem_object_flush_gpu_write_domain(obj);
  2278. if (ret)
  2279. return ret;
  2280. if (obj->pending_gpu_write || write) {
  2281. ret = i915_gem_object_wait_rendering(obj);
  2282. if (ret)
  2283. return ret;
  2284. }
  2285. i915_gem_object_flush_cpu_write_domain(obj);
  2286. old_write_domain = obj->base.write_domain;
  2287. old_read_domains = obj->base.read_domains;
  2288. /* It should now be out of any other write domains, and we can update
  2289. * the domain values for our changes.
  2290. */
  2291. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2292. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2293. if (write) {
  2294. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  2295. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  2296. obj->dirty = 1;
  2297. }
  2298. trace_i915_gem_object_change_domain(obj,
  2299. old_read_domains,
  2300. old_write_domain);
  2301. /* And bump the LRU for this access */
  2302. if (i915_gem_object_is_inactive(obj))
  2303. list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  2304. return 0;
  2305. }
  2306. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  2307. enum i915_cache_level cache_level)
  2308. {
  2309. struct drm_device *dev = obj->base.dev;
  2310. drm_i915_private_t *dev_priv = dev->dev_private;
  2311. int ret;
  2312. if (obj->cache_level == cache_level)
  2313. return 0;
  2314. if (obj->pin_count) {
  2315. DRM_DEBUG("can not change the cache level of pinned objects\n");
  2316. return -EBUSY;
  2317. }
  2318. if (obj->gtt_space) {
  2319. ret = i915_gem_object_finish_gpu(obj);
  2320. if (ret)
  2321. return ret;
  2322. i915_gem_object_finish_gtt(obj);
  2323. /* Before SandyBridge, you could not use tiling or fence
  2324. * registers with snooped memory, so relinquish any fences
  2325. * currently pointing to our region in the aperture.
  2326. */
  2327. if (INTEL_INFO(obj->base.dev)->gen < 6) {
  2328. ret = i915_gem_object_put_fence(obj);
  2329. if (ret)
  2330. return ret;
  2331. }
  2332. if (obj->has_global_gtt_mapping)
  2333. i915_gem_gtt_bind_object(obj, cache_level);
  2334. if (obj->has_aliasing_ppgtt_mapping)
  2335. i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
  2336. obj, cache_level);
  2337. }
  2338. if (cache_level == I915_CACHE_NONE) {
  2339. u32 old_read_domains, old_write_domain;
  2340. /* If we're coming from LLC cached, then we haven't
  2341. * actually been tracking whether the data is in the
  2342. * CPU cache or not, since we only allow one bit set
  2343. * in obj->write_domain and have been skipping the clflushes.
  2344. * Just set it to the CPU cache for now.
  2345. */
  2346. WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
  2347. WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
  2348. old_read_domains = obj->base.read_domains;
  2349. old_write_domain = obj->base.write_domain;
  2350. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2351. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2352. trace_i915_gem_object_change_domain(obj,
  2353. old_read_domains,
  2354. old_write_domain);
  2355. }
  2356. obj->cache_level = cache_level;
  2357. return 0;
  2358. }
  2359. /*
  2360. * Prepare buffer for display plane (scanout, cursors, etc).
  2361. * Can be called from an uninterruptible phase (modesetting) and allows
  2362. * any flushes to be pipelined (for pageflips).
  2363. */
  2364. int
  2365. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  2366. u32 alignment,
  2367. struct intel_ring_buffer *pipelined)
  2368. {
  2369. u32 old_read_domains, old_write_domain;
  2370. int ret;
  2371. ret = i915_gem_object_flush_gpu_write_domain(obj);
  2372. if (ret)
  2373. return ret;
  2374. if (pipelined != obj->ring) {
  2375. ret = i915_gem_object_sync(obj, pipelined);
  2376. if (ret)
  2377. return ret;
  2378. }
  2379. /* The display engine is not coherent with the LLC cache on gen6. As
  2380. * a result, we make sure that the pinning that is about to occur is
  2381. * done with uncached PTEs. This is lowest common denominator for all
  2382. * chipsets.
  2383. *
  2384. * However for gen6+, we could do better by using the GFDT bit instead
  2385. * of uncaching, which would allow us to flush all the LLC-cached data
  2386. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  2387. */
  2388. ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
  2389. if (ret)
  2390. return ret;
  2391. /* As the user may map the buffer once pinned in the display plane
  2392. * (e.g. libkms for the bootup splash), we have to ensure that we
  2393. * always use map_and_fenceable for all scanout buffers.
  2394. */
  2395. ret = i915_gem_object_pin(obj, alignment, true);
  2396. if (ret)
  2397. return ret;
  2398. i915_gem_object_flush_cpu_write_domain(obj);
  2399. old_write_domain = obj->base.write_domain;
  2400. old_read_domains = obj->base.read_domains;
  2401. /* It should now be out of any other write domains, and we can update
  2402. * the domain values for our changes.
  2403. */
  2404. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2405. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2406. trace_i915_gem_object_change_domain(obj,
  2407. old_read_domains,
  2408. old_write_domain);
  2409. return 0;
  2410. }
  2411. int
  2412. i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
  2413. {
  2414. int ret;
  2415. if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
  2416. return 0;
  2417. if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
  2418. ret = i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
  2419. if (ret)
  2420. return ret;
  2421. }
  2422. ret = i915_gem_object_wait_rendering(obj);
  2423. if (ret)
  2424. return ret;
  2425. /* Ensure that we invalidate the GPU's caches and TLBs. */
  2426. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  2427. return 0;
  2428. }
  2429. /**
  2430. * Moves a single object to the CPU read, and possibly write domain.
  2431. *
  2432. * This function returns when the move is complete, including waiting on
  2433. * flushes to occur.
  2434. */
  2435. int
  2436. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  2437. {
  2438. uint32_t old_write_domain, old_read_domains;
  2439. int ret;
  2440. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  2441. return 0;
  2442. ret = i915_gem_object_flush_gpu_write_domain(obj);
  2443. if (ret)
  2444. return ret;
  2445. if (write || obj->pending_gpu_write) {
  2446. ret = i915_gem_object_wait_rendering(obj);
  2447. if (ret)
  2448. return ret;
  2449. }
  2450. i915_gem_object_flush_gtt_write_domain(obj);
  2451. old_write_domain = obj->base.write_domain;
  2452. old_read_domains = obj->base.read_domains;
  2453. /* Flush the CPU cache if it's still invalid. */
  2454. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  2455. i915_gem_clflush_object(obj);
  2456. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  2457. }
  2458. /* It should now be out of any other write domains, and we can update
  2459. * the domain values for our changes.
  2460. */
  2461. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2462. /* If we're writing through the CPU, then the GPU read domains will
  2463. * need to be invalidated at next use.
  2464. */
  2465. if (write) {
  2466. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2467. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2468. }
  2469. trace_i915_gem_object_change_domain(obj,
  2470. old_read_domains,
  2471. old_write_domain);
  2472. return 0;
  2473. }
  2474. /* Throttle our rendering by waiting until the ring has completed our requests
  2475. * emitted over 20 msec ago.
  2476. *
  2477. * Note that if we were to use the current jiffies each time around the loop,
  2478. * we wouldn't escape the function with any frames outstanding if the time to
  2479. * render a frame was over 20ms.
  2480. *
  2481. * This should get us reasonable parallelism between CPU and GPU but also
  2482. * relatively low latency when blocking on a particular request to finish.
  2483. */
  2484. static int
  2485. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  2486. {
  2487. struct drm_i915_private *dev_priv = dev->dev_private;
  2488. struct drm_i915_file_private *file_priv = file->driver_priv;
  2489. unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
  2490. struct drm_i915_gem_request *request;
  2491. struct intel_ring_buffer *ring = NULL;
  2492. u32 seqno = 0;
  2493. int ret;
  2494. if (atomic_read(&dev_priv->mm.wedged))
  2495. return -EIO;
  2496. spin_lock(&file_priv->mm.lock);
  2497. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  2498. if (time_after_eq(request->emitted_jiffies, recent_enough))
  2499. break;
  2500. ring = request->ring;
  2501. seqno = request->seqno;
  2502. }
  2503. spin_unlock(&file_priv->mm.lock);
  2504. if (seqno == 0)
  2505. return 0;
  2506. ret = __wait_seqno(ring, seqno, true);
  2507. if (ret == 0)
  2508. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  2509. return ret;
  2510. }
  2511. int
  2512. i915_gem_object_pin(struct drm_i915_gem_object *obj,
  2513. uint32_t alignment,
  2514. bool map_and_fenceable)
  2515. {
  2516. int ret;
  2517. BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
  2518. if (obj->gtt_space != NULL) {
  2519. if ((alignment && obj->gtt_offset & (alignment - 1)) ||
  2520. (map_and_fenceable && !obj->map_and_fenceable)) {
  2521. WARN(obj->pin_count,
  2522. "bo is already pinned with incorrect alignment:"
  2523. " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
  2524. " obj->map_and_fenceable=%d\n",
  2525. obj->gtt_offset, alignment,
  2526. map_and_fenceable,
  2527. obj->map_and_fenceable);
  2528. ret = i915_gem_object_unbind(obj);
  2529. if (ret)
  2530. return ret;
  2531. }
  2532. }
  2533. if (obj->gtt_space == NULL) {
  2534. ret = i915_gem_object_bind_to_gtt(obj, alignment,
  2535. map_and_fenceable);
  2536. if (ret)
  2537. return ret;
  2538. }
  2539. if (!obj->has_global_gtt_mapping && map_and_fenceable)
  2540. i915_gem_gtt_bind_object(obj, obj->cache_level);
  2541. obj->pin_count++;
  2542. obj->pin_mappable |= map_and_fenceable;
  2543. return 0;
  2544. }
  2545. void
  2546. i915_gem_object_unpin(struct drm_i915_gem_object *obj)
  2547. {
  2548. BUG_ON(obj->pin_count == 0);
  2549. BUG_ON(obj->gtt_space == NULL);
  2550. if (--obj->pin_count == 0)
  2551. obj->pin_mappable = false;
  2552. }
  2553. int
  2554. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  2555. struct drm_file *file)
  2556. {
  2557. struct drm_i915_gem_pin *args = data;
  2558. struct drm_i915_gem_object *obj;
  2559. int ret;
  2560. ret = i915_mutex_lock_interruptible(dev);
  2561. if (ret)
  2562. return ret;
  2563. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2564. if (&obj->base == NULL) {
  2565. ret = -ENOENT;
  2566. goto unlock;
  2567. }
  2568. if (obj->madv != I915_MADV_WILLNEED) {
  2569. DRM_ERROR("Attempting to pin a purgeable buffer\n");
  2570. ret = -EINVAL;
  2571. goto out;
  2572. }
  2573. if (obj->pin_filp != NULL && obj->pin_filp != file) {
  2574. DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
  2575. args->handle);
  2576. ret = -EINVAL;
  2577. goto out;
  2578. }
  2579. obj->user_pin_count++;
  2580. obj->pin_filp = file;
  2581. if (obj->user_pin_count == 1) {
  2582. ret = i915_gem_object_pin(obj, args->alignment, true);
  2583. if (ret)
  2584. goto out;
  2585. }
  2586. /* XXX - flush the CPU caches for pinned objects
  2587. * as the X server doesn't manage domains yet
  2588. */
  2589. i915_gem_object_flush_cpu_write_domain(obj);
  2590. args->offset = obj->gtt_offset;
  2591. out:
  2592. drm_gem_object_unreference(&obj->base);
  2593. unlock:
  2594. mutex_unlock(&dev->struct_mutex);
  2595. return ret;
  2596. }
  2597. int
  2598. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  2599. struct drm_file *file)
  2600. {
  2601. struct drm_i915_gem_pin *args = data;
  2602. struct drm_i915_gem_object *obj;
  2603. int ret;
  2604. ret = i915_mutex_lock_interruptible(dev);
  2605. if (ret)
  2606. return ret;
  2607. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2608. if (&obj->base == NULL) {
  2609. ret = -ENOENT;
  2610. goto unlock;
  2611. }
  2612. if (obj->pin_filp != file) {
  2613. DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  2614. args->handle);
  2615. ret = -EINVAL;
  2616. goto out;
  2617. }
  2618. obj->user_pin_count--;
  2619. if (obj->user_pin_count == 0) {
  2620. obj->pin_filp = NULL;
  2621. i915_gem_object_unpin(obj);
  2622. }
  2623. out:
  2624. drm_gem_object_unreference(&obj->base);
  2625. unlock:
  2626. mutex_unlock(&dev->struct_mutex);
  2627. return ret;
  2628. }
  2629. int
  2630. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  2631. struct drm_file *file)
  2632. {
  2633. struct drm_i915_gem_busy *args = data;
  2634. struct drm_i915_gem_object *obj;
  2635. int ret;
  2636. ret = i915_mutex_lock_interruptible(dev);
  2637. if (ret)
  2638. return ret;
  2639. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2640. if (&obj->base == NULL) {
  2641. ret = -ENOENT;
  2642. goto unlock;
  2643. }
  2644. /* Count all active objects as busy, even if they are currently not used
  2645. * by the gpu. Users of this interface expect objects to eventually
  2646. * become non-busy without any further actions, therefore emit any
  2647. * necessary flushes here.
  2648. */
  2649. args->busy = obj->active;
  2650. if (args->busy) {
  2651. /* Unconditionally flush objects, even when the gpu still uses this
  2652. * object. Userspace calling this function indicates that it wants to
  2653. * use this buffer rather sooner than later, so issuing the required
  2654. * flush earlier is beneficial.
  2655. */
  2656. if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
  2657. ret = i915_gem_flush_ring(obj->ring,
  2658. 0, obj->base.write_domain);
  2659. } else {
  2660. ret = i915_gem_check_olr(obj->ring,
  2661. obj->last_rendering_seqno);
  2662. }
  2663. /* Update the active list for the hardware's current position.
  2664. * Otherwise this only updates on a delayed timer or when irqs
  2665. * are actually unmasked, and our working set ends up being
  2666. * larger than required.
  2667. */
  2668. i915_gem_retire_requests_ring(obj->ring);
  2669. args->busy = obj->active;
  2670. }
  2671. drm_gem_object_unreference(&obj->base);
  2672. unlock:
  2673. mutex_unlock(&dev->struct_mutex);
  2674. return ret;
  2675. }
  2676. int
  2677. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  2678. struct drm_file *file_priv)
  2679. {
  2680. return i915_gem_ring_throttle(dev, file_priv);
  2681. }
  2682. int
  2683. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  2684. struct drm_file *file_priv)
  2685. {
  2686. struct drm_i915_gem_madvise *args = data;
  2687. struct drm_i915_gem_object *obj;
  2688. int ret;
  2689. switch (args->madv) {
  2690. case I915_MADV_DONTNEED:
  2691. case I915_MADV_WILLNEED:
  2692. break;
  2693. default:
  2694. return -EINVAL;
  2695. }
  2696. ret = i915_mutex_lock_interruptible(dev);
  2697. if (ret)
  2698. return ret;
  2699. obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
  2700. if (&obj->base == NULL) {
  2701. ret = -ENOENT;
  2702. goto unlock;
  2703. }
  2704. if (obj->pin_count) {
  2705. ret = -EINVAL;
  2706. goto out;
  2707. }
  2708. if (obj->madv != __I915_MADV_PURGED)
  2709. obj->madv = args->madv;
  2710. /* if the object is no longer bound, discard its backing storage */
  2711. if (i915_gem_object_is_purgeable(obj) &&
  2712. obj->gtt_space == NULL)
  2713. i915_gem_object_truncate(obj);
  2714. args->retained = obj->madv != __I915_MADV_PURGED;
  2715. out:
  2716. drm_gem_object_unreference(&obj->base);
  2717. unlock:
  2718. mutex_unlock(&dev->struct_mutex);
  2719. return ret;
  2720. }
  2721. struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
  2722. size_t size)
  2723. {
  2724. struct drm_i915_private *dev_priv = dev->dev_private;
  2725. struct drm_i915_gem_object *obj;
  2726. struct address_space *mapping;
  2727. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  2728. if (obj == NULL)
  2729. return NULL;
  2730. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  2731. kfree(obj);
  2732. return NULL;
  2733. }
  2734. mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  2735. mapping_set_gfp_mask(mapping, GFP_HIGHUSER | __GFP_RECLAIMABLE);
  2736. i915_gem_info_add_obj(dev_priv, size);
  2737. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2738. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2739. if (HAS_LLC(dev)) {
  2740. /* On some devices, we can have the GPU use the LLC (the CPU
  2741. * cache) for about a 10% performance improvement
  2742. * compared to uncached. Graphics requests other than
  2743. * display scanout are coherent with the CPU in
  2744. * accessing this cache. This means in this mode we
  2745. * don't need to clflush on the CPU side, and on the
  2746. * GPU side we only need to flush internal caches to
  2747. * get data visible to the CPU.
  2748. *
  2749. * However, we maintain the display planes as UC, and so
  2750. * need to rebind when first used as such.
  2751. */
  2752. obj->cache_level = I915_CACHE_LLC;
  2753. } else
  2754. obj->cache_level = I915_CACHE_NONE;
  2755. obj->base.driver_private = NULL;
  2756. obj->fence_reg = I915_FENCE_REG_NONE;
  2757. INIT_LIST_HEAD(&obj->mm_list);
  2758. INIT_LIST_HEAD(&obj->gtt_list);
  2759. INIT_LIST_HEAD(&obj->ring_list);
  2760. INIT_LIST_HEAD(&obj->exec_list);
  2761. INIT_LIST_HEAD(&obj->gpu_write_list);
  2762. obj->madv = I915_MADV_WILLNEED;
  2763. /* Avoid an unnecessary call to unbind on the first bind. */
  2764. obj->map_and_fenceable = true;
  2765. return obj;
  2766. }
  2767. int i915_gem_init_object(struct drm_gem_object *obj)
  2768. {
  2769. BUG();
  2770. return 0;
  2771. }
  2772. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  2773. {
  2774. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  2775. struct drm_device *dev = obj->base.dev;
  2776. drm_i915_private_t *dev_priv = dev->dev_private;
  2777. trace_i915_gem_object_destroy(obj);
  2778. if (gem_obj->import_attach)
  2779. drm_prime_gem_destroy(gem_obj, obj->sg_table);
  2780. if (obj->phys_obj)
  2781. i915_gem_detach_phys_object(dev, obj);
  2782. obj->pin_count = 0;
  2783. if (WARN_ON(i915_gem_object_unbind(obj) == -ERESTARTSYS)) {
  2784. bool was_interruptible;
  2785. was_interruptible = dev_priv->mm.interruptible;
  2786. dev_priv->mm.interruptible = false;
  2787. WARN_ON(i915_gem_object_unbind(obj));
  2788. dev_priv->mm.interruptible = was_interruptible;
  2789. }
  2790. if (obj->base.map_list.map)
  2791. drm_gem_free_mmap_offset(&obj->base);
  2792. drm_gem_object_release(&obj->base);
  2793. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  2794. kfree(obj->bit_17);
  2795. kfree(obj);
  2796. }
  2797. int
  2798. i915_gem_idle(struct drm_device *dev)
  2799. {
  2800. drm_i915_private_t *dev_priv = dev->dev_private;
  2801. int ret;
  2802. mutex_lock(&dev->struct_mutex);
  2803. if (dev_priv->mm.suspended) {
  2804. mutex_unlock(&dev->struct_mutex);
  2805. return 0;
  2806. }
  2807. ret = i915_gpu_idle(dev);
  2808. if (ret) {
  2809. mutex_unlock(&dev->struct_mutex);
  2810. return ret;
  2811. }
  2812. i915_gem_retire_requests(dev);
  2813. /* Under UMS, be paranoid and evict. */
  2814. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2815. i915_gem_evict_everything(dev, false);
  2816. i915_gem_reset_fences(dev);
  2817. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  2818. * We need to replace this with a semaphore, or something.
  2819. * And not confound mm.suspended!
  2820. */
  2821. dev_priv->mm.suspended = 1;
  2822. del_timer_sync(&dev_priv->hangcheck_timer);
  2823. i915_kernel_lost_context(dev);
  2824. i915_gem_cleanup_ringbuffer(dev);
  2825. mutex_unlock(&dev->struct_mutex);
  2826. /* Cancel the retire work handler, which should be idle now. */
  2827. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  2828. return 0;
  2829. }
  2830. void i915_gem_init_swizzling(struct drm_device *dev)
  2831. {
  2832. drm_i915_private_t *dev_priv = dev->dev_private;
  2833. if (INTEL_INFO(dev)->gen < 5 ||
  2834. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  2835. return;
  2836. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  2837. DISP_TILE_SURFACE_SWIZZLING);
  2838. if (IS_GEN5(dev))
  2839. return;
  2840. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  2841. if (IS_GEN6(dev))
  2842. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  2843. else
  2844. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  2845. }
  2846. void i915_gem_init_ppgtt(struct drm_device *dev)
  2847. {
  2848. drm_i915_private_t *dev_priv = dev->dev_private;
  2849. uint32_t pd_offset;
  2850. struct intel_ring_buffer *ring;
  2851. struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
  2852. uint32_t __iomem *pd_addr;
  2853. uint32_t pd_entry;
  2854. int i;
  2855. if (!dev_priv->mm.aliasing_ppgtt)
  2856. return;
  2857. pd_addr = dev_priv->mm.gtt->gtt + ppgtt->pd_offset/sizeof(uint32_t);
  2858. for (i = 0; i < ppgtt->num_pd_entries; i++) {
  2859. dma_addr_t pt_addr;
  2860. if (dev_priv->mm.gtt->needs_dmar)
  2861. pt_addr = ppgtt->pt_dma_addr[i];
  2862. else
  2863. pt_addr = page_to_phys(ppgtt->pt_pages[i]);
  2864. pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
  2865. pd_entry |= GEN6_PDE_VALID;
  2866. writel(pd_entry, pd_addr + i);
  2867. }
  2868. readl(pd_addr);
  2869. pd_offset = ppgtt->pd_offset;
  2870. pd_offset /= 64; /* in cachelines, */
  2871. pd_offset <<= 16;
  2872. if (INTEL_INFO(dev)->gen == 6) {
  2873. uint32_t ecochk, gab_ctl, ecobits;
  2874. ecobits = I915_READ(GAC_ECO_BITS);
  2875. I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
  2876. gab_ctl = I915_READ(GAB_CTL);
  2877. I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
  2878. ecochk = I915_READ(GAM_ECOCHK);
  2879. I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT |
  2880. ECOCHK_PPGTT_CACHE64B);
  2881. I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
  2882. } else if (INTEL_INFO(dev)->gen >= 7) {
  2883. I915_WRITE(GAM_ECOCHK, ECOCHK_PPGTT_CACHE64B);
  2884. /* GFX_MODE is per-ring on gen7+ */
  2885. }
  2886. for_each_ring(ring, dev_priv, i) {
  2887. if (INTEL_INFO(dev)->gen >= 7)
  2888. I915_WRITE(RING_MODE_GEN7(ring),
  2889. _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
  2890. I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
  2891. I915_WRITE(RING_PP_DIR_BASE(ring), pd_offset);
  2892. }
  2893. }
  2894. int
  2895. i915_gem_init_hw(struct drm_device *dev)
  2896. {
  2897. drm_i915_private_t *dev_priv = dev->dev_private;
  2898. int ret;
  2899. i915_gem_init_swizzling(dev);
  2900. ret = intel_init_render_ring_buffer(dev);
  2901. if (ret)
  2902. return ret;
  2903. if (HAS_BSD(dev)) {
  2904. ret = intel_init_bsd_ring_buffer(dev);
  2905. if (ret)
  2906. goto cleanup_render_ring;
  2907. }
  2908. if (HAS_BLT(dev)) {
  2909. ret = intel_init_blt_ring_buffer(dev);
  2910. if (ret)
  2911. goto cleanup_bsd_ring;
  2912. }
  2913. dev_priv->next_seqno = 1;
  2914. i915_gem_init_ppgtt(dev);
  2915. return 0;
  2916. cleanup_bsd_ring:
  2917. intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
  2918. cleanup_render_ring:
  2919. intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
  2920. return ret;
  2921. }
  2922. static bool
  2923. intel_enable_ppgtt(struct drm_device *dev)
  2924. {
  2925. if (i915_enable_ppgtt >= 0)
  2926. return i915_enable_ppgtt;
  2927. #ifdef CONFIG_INTEL_IOMMU
  2928. /* Disable ppgtt on SNB if VT-d is on. */
  2929. if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped)
  2930. return false;
  2931. #endif
  2932. return true;
  2933. }
  2934. int i915_gem_init(struct drm_device *dev)
  2935. {
  2936. struct drm_i915_private *dev_priv = dev->dev_private;
  2937. unsigned long gtt_size, mappable_size;
  2938. int ret;
  2939. gtt_size = dev_priv->mm.gtt->gtt_total_entries << PAGE_SHIFT;
  2940. mappable_size = dev_priv->mm.gtt->gtt_mappable_entries << PAGE_SHIFT;
  2941. mutex_lock(&dev->struct_mutex);
  2942. if (intel_enable_ppgtt(dev) && HAS_ALIASING_PPGTT(dev)) {
  2943. /* PPGTT pdes are stolen from global gtt ptes, so shrink the
  2944. * aperture accordingly when using aliasing ppgtt. */
  2945. gtt_size -= I915_PPGTT_PD_ENTRIES*PAGE_SIZE;
  2946. i915_gem_init_global_gtt(dev, 0, mappable_size, gtt_size);
  2947. ret = i915_gem_init_aliasing_ppgtt(dev);
  2948. if (ret) {
  2949. mutex_unlock(&dev->struct_mutex);
  2950. return ret;
  2951. }
  2952. } else {
  2953. /* Let GEM Manage all of the aperture.
  2954. *
  2955. * However, leave one page at the end still bound to the scratch
  2956. * page. There are a number of places where the hardware
  2957. * apparently prefetches past the end of the object, and we've
  2958. * seen multiple hangs with the GPU head pointer stuck in a
  2959. * batchbuffer bound at the last page of the aperture. One page
  2960. * should be enough to keep any prefetching inside of the
  2961. * aperture.
  2962. */
  2963. i915_gem_init_global_gtt(dev, 0, mappable_size,
  2964. gtt_size);
  2965. }
  2966. ret = i915_gem_init_hw(dev);
  2967. mutex_unlock(&dev->struct_mutex);
  2968. if (ret) {
  2969. i915_gem_cleanup_aliasing_ppgtt(dev);
  2970. return ret;
  2971. }
  2972. /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */
  2973. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2974. dev_priv->dri1.allow_batchbuffer = 1;
  2975. return 0;
  2976. }
  2977. void
  2978. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  2979. {
  2980. drm_i915_private_t *dev_priv = dev->dev_private;
  2981. struct intel_ring_buffer *ring;
  2982. int i;
  2983. for_each_ring(ring, dev_priv, i)
  2984. intel_cleanup_ring_buffer(ring);
  2985. }
  2986. int
  2987. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  2988. struct drm_file *file_priv)
  2989. {
  2990. drm_i915_private_t *dev_priv = dev->dev_private;
  2991. int ret;
  2992. if (drm_core_check_feature(dev, DRIVER_MODESET))
  2993. return 0;
  2994. if (atomic_read(&dev_priv->mm.wedged)) {
  2995. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  2996. atomic_set(&dev_priv->mm.wedged, 0);
  2997. }
  2998. mutex_lock(&dev->struct_mutex);
  2999. dev_priv->mm.suspended = 0;
  3000. ret = i915_gem_init_hw(dev);
  3001. if (ret != 0) {
  3002. mutex_unlock(&dev->struct_mutex);
  3003. return ret;
  3004. }
  3005. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  3006. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  3007. BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
  3008. mutex_unlock(&dev->struct_mutex);
  3009. ret = drm_irq_install(dev);
  3010. if (ret)
  3011. goto cleanup_ringbuffer;
  3012. return 0;
  3013. cleanup_ringbuffer:
  3014. mutex_lock(&dev->struct_mutex);
  3015. i915_gem_cleanup_ringbuffer(dev);
  3016. dev_priv->mm.suspended = 1;
  3017. mutex_unlock(&dev->struct_mutex);
  3018. return ret;
  3019. }
  3020. int
  3021. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  3022. struct drm_file *file_priv)
  3023. {
  3024. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3025. return 0;
  3026. drm_irq_uninstall(dev);
  3027. return i915_gem_idle(dev);
  3028. }
  3029. void
  3030. i915_gem_lastclose(struct drm_device *dev)
  3031. {
  3032. int ret;
  3033. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3034. return;
  3035. ret = i915_gem_idle(dev);
  3036. if (ret)
  3037. DRM_ERROR("failed to idle hardware: %d\n", ret);
  3038. }
  3039. static void
  3040. init_ring_lists(struct intel_ring_buffer *ring)
  3041. {
  3042. INIT_LIST_HEAD(&ring->active_list);
  3043. INIT_LIST_HEAD(&ring->request_list);
  3044. INIT_LIST_HEAD(&ring->gpu_write_list);
  3045. }
  3046. void
  3047. i915_gem_load(struct drm_device *dev)
  3048. {
  3049. int i;
  3050. drm_i915_private_t *dev_priv = dev->dev_private;
  3051. INIT_LIST_HEAD(&dev_priv->mm.active_list);
  3052. INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
  3053. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  3054. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  3055. INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
  3056. for (i = 0; i < I915_NUM_RINGS; i++)
  3057. init_ring_lists(&dev_priv->ring[i]);
  3058. for (i = 0; i < I915_MAX_NUM_FENCES; i++)
  3059. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  3060. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  3061. i915_gem_retire_work_handler);
  3062. init_completion(&dev_priv->error_completion);
  3063. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  3064. if (IS_GEN3(dev)) {
  3065. I915_WRITE(MI_ARB_STATE,
  3066. _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
  3067. }
  3068. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  3069. /* Old X drivers will take 0-2 for front, back, depth buffers */
  3070. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3071. dev_priv->fence_reg_start = 3;
  3072. if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3073. dev_priv->num_fence_regs = 16;
  3074. else
  3075. dev_priv->num_fence_regs = 8;
  3076. /* Initialize fence registers to zero */
  3077. i915_gem_reset_fences(dev);
  3078. i915_gem_detect_bit_6_swizzle(dev);
  3079. init_waitqueue_head(&dev_priv->pending_flip_queue);
  3080. dev_priv->mm.interruptible = true;
  3081. dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
  3082. dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
  3083. register_shrinker(&dev_priv->mm.inactive_shrinker);
  3084. }
  3085. /*
  3086. * Create a physically contiguous memory object for this object
  3087. * e.g. for cursor + overlay regs
  3088. */
  3089. static int i915_gem_init_phys_object(struct drm_device *dev,
  3090. int id, int size, int align)
  3091. {
  3092. drm_i915_private_t *dev_priv = dev->dev_private;
  3093. struct drm_i915_gem_phys_object *phys_obj;
  3094. int ret;
  3095. if (dev_priv->mm.phys_objs[id - 1] || !size)
  3096. return 0;
  3097. phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
  3098. if (!phys_obj)
  3099. return -ENOMEM;
  3100. phys_obj->id = id;
  3101. phys_obj->handle = drm_pci_alloc(dev, size, align);
  3102. if (!phys_obj->handle) {
  3103. ret = -ENOMEM;
  3104. goto kfree_obj;
  3105. }
  3106. #ifdef CONFIG_X86
  3107. set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3108. #endif
  3109. dev_priv->mm.phys_objs[id - 1] = phys_obj;
  3110. return 0;
  3111. kfree_obj:
  3112. kfree(phys_obj);
  3113. return ret;
  3114. }
  3115. static void i915_gem_free_phys_object(struct drm_device *dev, int id)
  3116. {
  3117. drm_i915_private_t *dev_priv = dev->dev_private;
  3118. struct drm_i915_gem_phys_object *phys_obj;
  3119. if (!dev_priv->mm.phys_objs[id - 1])
  3120. return;
  3121. phys_obj = dev_priv->mm.phys_objs[id - 1];
  3122. if (phys_obj->cur_obj) {
  3123. i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
  3124. }
  3125. #ifdef CONFIG_X86
  3126. set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3127. #endif
  3128. drm_pci_free(dev, phys_obj->handle);
  3129. kfree(phys_obj);
  3130. dev_priv->mm.phys_objs[id - 1] = NULL;
  3131. }
  3132. void i915_gem_free_all_phys_object(struct drm_device *dev)
  3133. {
  3134. int i;
  3135. for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
  3136. i915_gem_free_phys_object(dev, i);
  3137. }
  3138. void i915_gem_detach_phys_object(struct drm_device *dev,
  3139. struct drm_i915_gem_object *obj)
  3140. {
  3141. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  3142. char *vaddr;
  3143. int i;
  3144. int page_count;
  3145. if (!obj->phys_obj)
  3146. return;
  3147. vaddr = obj->phys_obj->handle->vaddr;
  3148. page_count = obj->base.size / PAGE_SIZE;
  3149. for (i = 0; i < page_count; i++) {
  3150. struct page *page = shmem_read_mapping_page(mapping, i);
  3151. if (!IS_ERR(page)) {
  3152. char *dst = kmap_atomic(page);
  3153. memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
  3154. kunmap_atomic(dst);
  3155. drm_clflush_pages(&page, 1);
  3156. set_page_dirty(page);
  3157. mark_page_accessed(page);
  3158. page_cache_release(page);
  3159. }
  3160. }
  3161. intel_gtt_chipset_flush();
  3162. obj->phys_obj->cur_obj = NULL;
  3163. obj->phys_obj = NULL;
  3164. }
  3165. int
  3166. i915_gem_attach_phys_object(struct drm_device *dev,
  3167. struct drm_i915_gem_object *obj,
  3168. int id,
  3169. int align)
  3170. {
  3171. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  3172. drm_i915_private_t *dev_priv = dev->dev_private;
  3173. int ret = 0;
  3174. int page_count;
  3175. int i;
  3176. if (id > I915_MAX_PHYS_OBJECT)
  3177. return -EINVAL;
  3178. if (obj->phys_obj) {
  3179. if (obj->phys_obj->id == id)
  3180. return 0;
  3181. i915_gem_detach_phys_object(dev, obj);
  3182. }
  3183. /* create a new object */
  3184. if (!dev_priv->mm.phys_objs[id - 1]) {
  3185. ret = i915_gem_init_phys_object(dev, id,
  3186. obj->base.size, align);
  3187. if (ret) {
  3188. DRM_ERROR("failed to init phys object %d size: %zu\n",
  3189. id, obj->base.size);
  3190. return ret;
  3191. }
  3192. }
  3193. /* bind to the object */
  3194. obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
  3195. obj->phys_obj->cur_obj = obj;
  3196. page_count = obj->base.size / PAGE_SIZE;
  3197. for (i = 0; i < page_count; i++) {
  3198. struct page *page;
  3199. char *dst, *src;
  3200. page = shmem_read_mapping_page(mapping, i);
  3201. if (IS_ERR(page))
  3202. return PTR_ERR(page);
  3203. src = kmap_atomic(page);
  3204. dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  3205. memcpy(dst, src, PAGE_SIZE);
  3206. kunmap_atomic(src);
  3207. mark_page_accessed(page);
  3208. page_cache_release(page);
  3209. }
  3210. return 0;
  3211. }
  3212. static int
  3213. i915_gem_phys_pwrite(struct drm_device *dev,
  3214. struct drm_i915_gem_object *obj,
  3215. struct drm_i915_gem_pwrite *args,
  3216. struct drm_file *file_priv)
  3217. {
  3218. void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
  3219. char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
  3220. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  3221. unsigned long unwritten;
  3222. /* The physical object once assigned is fixed for the lifetime
  3223. * of the obj, so we can safely drop the lock and continue
  3224. * to access vaddr.
  3225. */
  3226. mutex_unlock(&dev->struct_mutex);
  3227. unwritten = copy_from_user(vaddr, user_data, args->size);
  3228. mutex_lock(&dev->struct_mutex);
  3229. if (unwritten)
  3230. return -EFAULT;
  3231. }
  3232. intel_gtt_chipset_flush();
  3233. return 0;
  3234. }
  3235. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  3236. {
  3237. struct drm_i915_file_private *file_priv = file->driver_priv;
  3238. /* Clean up our request list when the client is going away, so that
  3239. * later retire_requests won't dereference our soon-to-be-gone
  3240. * file_priv.
  3241. */
  3242. spin_lock(&file_priv->mm.lock);
  3243. while (!list_empty(&file_priv->mm.request_list)) {
  3244. struct drm_i915_gem_request *request;
  3245. request = list_first_entry(&file_priv->mm.request_list,
  3246. struct drm_i915_gem_request,
  3247. client_list);
  3248. list_del(&request->client_list);
  3249. request->file_priv = NULL;
  3250. }
  3251. spin_unlock(&file_priv->mm.lock);
  3252. }
  3253. static int
  3254. i915_gpu_is_active(struct drm_device *dev)
  3255. {
  3256. drm_i915_private_t *dev_priv = dev->dev_private;
  3257. int lists_empty;
  3258. lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
  3259. list_empty(&dev_priv->mm.active_list);
  3260. return !lists_empty;
  3261. }
  3262. static int
  3263. i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
  3264. {
  3265. struct drm_i915_private *dev_priv =
  3266. container_of(shrinker,
  3267. struct drm_i915_private,
  3268. mm.inactive_shrinker);
  3269. struct drm_device *dev = dev_priv->dev;
  3270. struct drm_i915_gem_object *obj, *next;
  3271. int nr_to_scan = sc->nr_to_scan;
  3272. int cnt;
  3273. if (!mutex_trylock(&dev->struct_mutex))
  3274. return 0;
  3275. /* "fast-path" to count number of available objects */
  3276. if (nr_to_scan == 0) {
  3277. cnt = 0;
  3278. list_for_each_entry(obj,
  3279. &dev_priv->mm.inactive_list,
  3280. mm_list)
  3281. cnt++;
  3282. mutex_unlock(&dev->struct_mutex);
  3283. return cnt / 100 * sysctl_vfs_cache_pressure;
  3284. }
  3285. rescan:
  3286. /* first scan for clean buffers */
  3287. i915_gem_retire_requests(dev);
  3288. list_for_each_entry_safe(obj, next,
  3289. &dev_priv->mm.inactive_list,
  3290. mm_list) {
  3291. if (i915_gem_object_is_purgeable(obj)) {
  3292. if (i915_gem_object_unbind(obj) == 0 &&
  3293. --nr_to_scan == 0)
  3294. break;
  3295. }
  3296. }
  3297. /* second pass, evict/count anything still on the inactive list */
  3298. cnt = 0;
  3299. list_for_each_entry_safe(obj, next,
  3300. &dev_priv->mm.inactive_list,
  3301. mm_list) {
  3302. if (nr_to_scan &&
  3303. i915_gem_object_unbind(obj) == 0)
  3304. nr_to_scan--;
  3305. else
  3306. cnt++;
  3307. }
  3308. if (nr_to_scan && i915_gpu_is_active(dev)) {
  3309. /*
  3310. * We are desperate for pages, so as a last resort, wait
  3311. * for the GPU to finish and discard whatever we can.
  3312. * This has a dramatic impact to reduce the number of
  3313. * OOM-killer events whilst running the GPU aggressively.
  3314. */
  3315. if (i915_gpu_idle(dev) == 0)
  3316. goto rescan;
  3317. }
  3318. mutex_unlock(&dev->struct_mutex);
  3319. return cnt / 100 * sysctl_vfs_cache_pressure;
  3320. }