i915_gem.c 134 KB

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