i915_gem.c 124 KB

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