i915_gem.c 125 KB

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