i915_gem.c 111 KB

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