i915_gem.c 122 KB

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