i915_gem.c 141 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273
  1. /*
  2. * Copyright © 2008-2015 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_gem_clflush.h"
  32. #include "i915_vgpu.h"
  33. #include "i915_trace.h"
  34. #include "intel_drv.h"
  35. #include "intel_frontbuffer.h"
  36. #include "intel_mocs.h"
  37. #include <linux/dma-fence-array.h>
  38. #include <linux/kthread.h>
  39. #include <linux/reservation.h>
  40. #include <linux/shmem_fs.h>
  41. #include <linux/slab.h>
  42. #include <linux/stop_machine.h>
  43. #include <linux/swap.h>
  44. #include <linux/pci.h>
  45. #include <linux/dma-buf.h>
  46. static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
  47. static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  48. {
  49. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  50. return false;
  51. if (!i915_gem_object_is_coherent(obj))
  52. return true;
  53. return obj->pin_display;
  54. }
  55. static int
  56. insert_mappable_node(struct i915_ggtt *ggtt,
  57. struct drm_mm_node *node, u32 size)
  58. {
  59. memset(node, 0, sizeof(*node));
  60. return drm_mm_insert_node_in_range(&ggtt->base.mm, node,
  61. size, 0, I915_COLOR_UNEVICTABLE,
  62. 0, ggtt->mappable_end,
  63. DRM_MM_INSERT_LOW);
  64. }
  65. static void
  66. remove_mappable_node(struct drm_mm_node *node)
  67. {
  68. drm_mm_remove_node(node);
  69. }
  70. /* some bookkeeping */
  71. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  72. u64 size)
  73. {
  74. spin_lock(&dev_priv->mm.object_stat_lock);
  75. dev_priv->mm.object_count++;
  76. dev_priv->mm.object_memory += size;
  77. spin_unlock(&dev_priv->mm.object_stat_lock);
  78. }
  79. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  80. u64 size)
  81. {
  82. spin_lock(&dev_priv->mm.object_stat_lock);
  83. dev_priv->mm.object_count--;
  84. dev_priv->mm.object_memory -= size;
  85. spin_unlock(&dev_priv->mm.object_stat_lock);
  86. }
  87. static int
  88. i915_gem_wait_for_error(struct i915_gpu_error *error)
  89. {
  90. int ret;
  91. might_sleep();
  92. /*
  93. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  94. * userspace. If it takes that long something really bad is going on and
  95. * we should simply try to bail out and fail as gracefully as possible.
  96. */
  97. ret = wait_event_interruptible_timeout(error->reset_queue,
  98. !i915_reset_backoff(error),
  99. I915_RESET_TIMEOUT);
  100. if (ret == 0) {
  101. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  102. return -EIO;
  103. } else if (ret < 0) {
  104. return ret;
  105. } else {
  106. return 0;
  107. }
  108. }
  109. int i915_mutex_lock_interruptible(struct drm_device *dev)
  110. {
  111. struct drm_i915_private *dev_priv = to_i915(dev);
  112. int ret;
  113. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  114. if (ret)
  115. return ret;
  116. ret = mutex_lock_interruptible(&dev->struct_mutex);
  117. if (ret)
  118. return ret;
  119. return 0;
  120. }
  121. int
  122. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  123. struct drm_file *file)
  124. {
  125. struct drm_i915_private *dev_priv = to_i915(dev);
  126. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  127. struct drm_i915_gem_get_aperture *args = data;
  128. struct i915_vma *vma;
  129. size_t pinned;
  130. pinned = 0;
  131. mutex_lock(&dev->struct_mutex);
  132. list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
  133. if (i915_vma_is_pinned(vma))
  134. pinned += vma->node.size;
  135. list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
  136. if (i915_vma_is_pinned(vma))
  137. pinned += vma->node.size;
  138. mutex_unlock(&dev->struct_mutex);
  139. args->aper_size = ggtt->base.total;
  140. args->aper_available_size = args->aper_size - pinned;
  141. return 0;
  142. }
  143. static struct sg_table *
  144. i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
  145. {
  146. struct address_space *mapping = obj->base.filp->f_mapping;
  147. drm_dma_handle_t *phys;
  148. struct sg_table *st;
  149. struct scatterlist *sg;
  150. char *vaddr;
  151. int i;
  152. if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
  153. return ERR_PTR(-EINVAL);
  154. /* Always aligning to the object size, allows a single allocation
  155. * to handle all possible callers, and given typical object sizes,
  156. * the alignment of the buddy allocation will naturally match.
  157. */
  158. phys = drm_pci_alloc(obj->base.dev,
  159. obj->base.size,
  160. roundup_pow_of_two(obj->base.size));
  161. if (!phys)
  162. return ERR_PTR(-ENOMEM);
  163. vaddr = phys->vaddr;
  164. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  165. struct page *page;
  166. char *src;
  167. page = shmem_read_mapping_page(mapping, i);
  168. if (IS_ERR(page)) {
  169. st = ERR_CAST(page);
  170. goto err_phys;
  171. }
  172. src = kmap_atomic(page);
  173. memcpy(vaddr, src, PAGE_SIZE);
  174. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  175. kunmap_atomic(src);
  176. put_page(page);
  177. vaddr += PAGE_SIZE;
  178. }
  179. i915_gem_chipset_flush(to_i915(obj->base.dev));
  180. st = kmalloc(sizeof(*st), GFP_KERNEL);
  181. if (!st) {
  182. st = ERR_PTR(-ENOMEM);
  183. goto err_phys;
  184. }
  185. if (sg_alloc_table(st, 1, GFP_KERNEL)) {
  186. kfree(st);
  187. st = ERR_PTR(-ENOMEM);
  188. goto err_phys;
  189. }
  190. sg = st->sgl;
  191. sg->offset = 0;
  192. sg->length = obj->base.size;
  193. sg_dma_address(sg) = phys->busaddr;
  194. sg_dma_len(sg) = obj->base.size;
  195. obj->phys_handle = phys;
  196. return st;
  197. err_phys:
  198. drm_pci_free(obj->base.dev, phys);
  199. return st;
  200. }
  201. static void
  202. __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
  203. struct sg_table *pages,
  204. bool needs_clflush)
  205. {
  206. GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
  207. if (obj->mm.madv == I915_MADV_DONTNEED)
  208. obj->mm.dirty = false;
  209. if (needs_clflush &&
  210. (obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
  211. !i915_gem_object_is_coherent(obj))
  212. drm_clflush_sg(pages);
  213. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  214. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  215. }
  216. static void
  217. i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
  218. struct sg_table *pages)
  219. {
  220. __i915_gem_object_release_shmem(obj, pages, false);
  221. if (obj->mm.dirty) {
  222. struct address_space *mapping = obj->base.filp->f_mapping;
  223. char *vaddr = obj->phys_handle->vaddr;
  224. int i;
  225. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  226. struct page *page;
  227. char *dst;
  228. page = shmem_read_mapping_page(mapping, i);
  229. if (IS_ERR(page))
  230. continue;
  231. dst = kmap_atomic(page);
  232. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  233. memcpy(dst, vaddr, PAGE_SIZE);
  234. kunmap_atomic(dst);
  235. set_page_dirty(page);
  236. if (obj->mm.madv == I915_MADV_WILLNEED)
  237. mark_page_accessed(page);
  238. put_page(page);
  239. vaddr += PAGE_SIZE;
  240. }
  241. obj->mm.dirty = false;
  242. }
  243. sg_free_table(pages);
  244. kfree(pages);
  245. drm_pci_free(obj->base.dev, obj->phys_handle);
  246. }
  247. static void
  248. i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
  249. {
  250. i915_gem_object_unpin_pages(obj);
  251. }
  252. static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
  253. .get_pages = i915_gem_object_get_pages_phys,
  254. .put_pages = i915_gem_object_put_pages_phys,
  255. .release = i915_gem_object_release_phys,
  256. };
  257. static const struct drm_i915_gem_object_ops i915_gem_object_ops;
  258. int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
  259. {
  260. struct i915_vma *vma;
  261. LIST_HEAD(still_in_list);
  262. int ret;
  263. lockdep_assert_held(&obj->base.dev->struct_mutex);
  264. /* Closed vma are removed from the obj->vma_list - but they may
  265. * still have an active binding on the object. To remove those we
  266. * must wait for all rendering to complete to the object (as unbinding
  267. * must anyway), and retire the requests.
  268. */
  269. ret = i915_gem_object_wait(obj,
  270. I915_WAIT_INTERRUPTIBLE |
  271. I915_WAIT_LOCKED |
  272. I915_WAIT_ALL,
  273. MAX_SCHEDULE_TIMEOUT,
  274. NULL);
  275. if (ret)
  276. return ret;
  277. i915_gem_retire_requests(to_i915(obj->base.dev));
  278. while ((vma = list_first_entry_or_null(&obj->vma_list,
  279. struct i915_vma,
  280. obj_link))) {
  281. list_move_tail(&vma->obj_link, &still_in_list);
  282. ret = i915_vma_unbind(vma);
  283. if (ret)
  284. break;
  285. }
  286. list_splice(&still_in_list, &obj->vma_list);
  287. return ret;
  288. }
  289. static long
  290. i915_gem_object_wait_fence(struct dma_fence *fence,
  291. unsigned int flags,
  292. long timeout,
  293. struct intel_rps_client *rps)
  294. {
  295. struct drm_i915_gem_request *rq;
  296. BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
  297. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  298. return timeout;
  299. if (!dma_fence_is_i915(fence))
  300. return dma_fence_wait_timeout(fence,
  301. flags & I915_WAIT_INTERRUPTIBLE,
  302. timeout);
  303. rq = to_request(fence);
  304. if (i915_gem_request_completed(rq))
  305. goto out;
  306. /* This client is about to stall waiting for the GPU. In many cases
  307. * this is undesirable and limits the throughput of the system, as
  308. * many clients cannot continue processing user input/output whilst
  309. * blocked. RPS autotuning may take tens of milliseconds to respond
  310. * to the GPU load and thus incurs additional latency for the client.
  311. * We can circumvent that by promoting the GPU frequency to maximum
  312. * before we wait. This makes the GPU throttle up much more quickly
  313. * (good for benchmarks and user experience, e.g. window animations),
  314. * but at a cost of spending more power processing the workload
  315. * (bad for battery). Not all clients even want their results
  316. * immediately and for them we should just let the GPU select its own
  317. * frequency to maximise efficiency. To prevent a single client from
  318. * forcing the clocks too high for the whole system, we only allow
  319. * each client to waitboost once in a busy period.
  320. */
  321. if (rps) {
  322. if (INTEL_GEN(rq->i915) >= 6)
  323. gen6_rps_boost(rq->i915, rps, rq->emitted_jiffies);
  324. else
  325. rps = NULL;
  326. }
  327. timeout = i915_wait_request(rq, flags, timeout);
  328. out:
  329. if (flags & I915_WAIT_LOCKED && i915_gem_request_completed(rq))
  330. i915_gem_request_retire_upto(rq);
  331. if (rps && i915_gem_request_global_seqno(rq) == intel_engine_last_submit(rq->engine)) {
  332. /* The GPU is now idle and this client has stalled.
  333. * Since no other client has submitted a request in the
  334. * meantime, assume that this client is the only one
  335. * supplying work to the GPU but is unable to keep that
  336. * work supplied because it is waiting. Since the GPU is
  337. * then never kept fully busy, RPS autoclocking will
  338. * keep the clocks relatively low, causing further delays.
  339. * Compensate by giving the synchronous client credit for
  340. * a waitboost next time.
  341. */
  342. spin_lock(&rq->i915->rps.client_lock);
  343. list_del_init(&rps->link);
  344. spin_unlock(&rq->i915->rps.client_lock);
  345. }
  346. return timeout;
  347. }
  348. static long
  349. i915_gem_object_wait_reservation(struct reservation_object *resv,
  350. unsigned int flags,
  351. long timeout,
  352. struct intel_rps_client *rps)
  353. {
  354. unsigned int seq = __read_seqcount_begin(&resv->seq);
  355. struct dma_fence *excl;
  356. bool prune_fences = false;
  357. if (flags & I915_WAIT_ALL) {
  358. struct dma_fence **shared;
  359. unsigned int count, i;
  360. int ret;
  361. ret = reservation_object_get_fences_rcu(resv,
  362. &excl, &count, &shared);
  363. if (ret)
  364. return ret;
  365. for (i = 0; i < count; i++) {
  366. timeout = i915_gem_object_wait_fence(shared[i],
  367. flags, timeout,
  368. rps);
  369. if (timeout < 0)
  370. break;
  371. dma_fence_put(shared[i]);
  372. }
  373. for (; i < count; i++)
  374. dma_fence_put(shared[i]);
  375. kfree(shared);
  376. prune_fences = count && timeout >= 0;
  377. } else {
  378. excl = reservation_object_get_excl_rcu(resv);
  379. }
  380. if (excl && timeout >= 0) {
  381. timeout = i915_gem_object_wait_fence(excl, flags, timeout, rps);
  382. prune_fences = timeout >= 0;
  383. }
  384. dma_fence_put(excl);
  385. /* Oportunistically prune the fences iff we know they have *all* been
  386. * signaled and that the reservation object has not been changed (i.e.
  387. * no new fences have been added).
  388. */
  389. if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
  390. if (reservation_object_trylock(resv)) {
  391. if (!__read_seqcount_retry(&resv->seq, seq))
  392. reservation_object_add_excl_fence(resv, NULL);
  393. reservation_object_unlock(resv);
  394. }
  395. }
  396. return timeout;
  397. }
  398. static void __fence_set_priority(struct dma_fence *fence, int prio)
  399. {
  400. struct drm_i915_gem_request *rq;
  401. struct intel_engine_cs *engine;
  402. if (!dma_fence_is_i915(fence))
  403. return;
  404. rq = to_request(fence);
  405. engine = rq->engine;
  406. if (!engine->schedule)
  407. return;
  408. engine->schedule(rq, prio);
  409. }
  410. static void fence_set_priority(struct dma_fence *fence, int prio)
  411. {
  412. /* Recurse once into a fence-array */
  413. if (dma_fence_is_array(fence)) {
  414. struct dma_fence_array *array = to_dma_fence_array(fence);
  415. int i;
  416. for (i = 0; i < array->num_fences; i++)
  417. __fence_set_priority(array->fences[i], prio);
  418. } else {
  419. __fence_set_priority(fence, prio);
  420. }
  421. }
  422. int
  423. i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
  424. unsigned int flags,
  425. int prio)
  426. {
  427. struct dma_fence *excl;
  428. if (flags & I915_WAIT_ALL) {
  429. struct dma_fence **shared;
  430. unsigned int count, i;
  431. int ret;
  432. ret = reservation_object_get_fences_rcu(obj->resv,
  433. &excl, &count, &shared);
  434. if (ret)
  435. return ret;
  436. for (i = 0; i < count; i++) {
  437. fence_set_priority(shared[i], prio);
  438. dma_fence_put(shared[i]);
  439. }
  440. kfree(shared);
  441. } else {
  442. excl = reservation_object_get_excl_rcu(obj->resv);
  443. }
  444. if (excl) {
  445. fence_set_priority(excl, prio);
  446. dma_fence_put(excl);
  447. }
  448. return 0;
  449. }
  450. /**
  451. * Waits for rendering to the object to be completed
  452. * @obj: i915 gem object
  453. * @flags: how to wait (under a lock, for all rendering or just for writes etc)
  454. * @timeout: how long to wait
  455. * @rps: client (user process) to charge for any waitboosting
  456. */
  457. int
  458. i915_gem_object_wait(struct drm_i915_gem_object *obj,
  459. unsigned int flags,
  460. long timeout,
  461. struct intel_rps_client *rps)
  462. {
  463. might_sleep();
  464. #if IS_ENABLED(CONFIG_LOCKDEP)
  465. GEM_BUG_ON(debug_locks &&
  466. !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
  467. !!(flags & I915_WAIT_LOCKED));
  468. #endif
  469. GEM_BUG_ON(timeout < 0);
  470. timeout = i915_gem_object_wait_reservation(obj->resv,
  471. flags, timeout,
  472. rps);
  473. return timeout < 0 ? timeout : 0;
  474. }
  475. static struct intel_rps_client *to_rps_client(struct drm_file *file)
  476. {
  477. struct drm_i915_file_private *fpriv = file->driver_priv;
  478. return &fpriv->rps;
  479. }
  480. int
  481. i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
  482. int align)
  483. {
  484. int ret;
  485. if (align > obj->base.size)
  486. return -EINVAL;
  487. if (obj->ops == &i915_gem_phys_ops)
  488. return 0;
  489. if (obj->mm.madv != I915_MADV_WILLNEED)
  490. return -EFAULT;
  491. if (obj->base.filp == NULL)
  492. return -EINVAL;
  493. ret = i915_gem_object_unbind(obj);
  494. if (ret)
  495. return ret;
  496. __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
  497. if (obj->mm.pages)
  498. return -EBUSY;
  499. GEM_BUG_ON(obj->ops != &i915_gem_object_ops);
  500. obj->ops = &i915_gem_phys_ops;
  501. ret = i915_gem_object_pin_pages(obj);
  502. if (ret)
  503. goto err_xfer;
  504. return 0;
  505. err_xfer:
  506. obj->ops = &i915_gem_object_ops;
  507. return ret;
  508. }
  509. static int
  510. i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
  511. struct drm_i915_gem_pwrite *args,
  512. struct drm_file *file)
  513. {
  514. void *vaddr = obj->phys_handle->vaddr + args->offset;
  515. char __user *user_data = u64_to_user_ptr(args->data_ptr);
  516. /* We manually control the domain here and pretend that it
  517. * remains coherent i.e. in the GTT domain, like shmem_pwrite.
  518. */
  519. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  520. if (copy_from_user(vaddr, user_data, args->size))
  521. return -EFAULT;
  522. drm_clflush_virt_range(vaddr, args->size);
  523. i915_gem_chipset_flush(to_i915(obj->base.dev));
  524. intel_fb_obj_flush(obj, ORIGIN_CPU);
  525. return 0;
  526. }
  527. void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
  528. {
  529. return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
  530. }
  531. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  532. {
  533. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  534. kmem_cache_free(dev_priv->objects, obj);
  535. }
  536. static int
  537. i915_gem_create(struct drm_file *file,
  538. struct drm_i915_private *dev_priv,
  539. uint64_t size,
  540. uint32_t *handle_p)
  541. {
  542. struct drm_i915_gem_object *obj;
  543. int ret;
  544. u32 handle;
  545. size = roundup(size, PAGE_SIZE);
  546. if (size == 0)
  547. return -EINVAL;
  548. /* Allocate the new object */
  549. obj = i915_gem_object_create(dev_priv, size);
  550. if (IS_ERR(obj))
  551. return PTR_ERR(obj);
  552. ret = drm_gem_handle_create(file, &obj->base, &handle);
  553. /* drop reference from allocate - handle holds it now */
  554. i915_gem_object_put(obj);
  555. if (ret)
  556. return ret;
  557. *handle_p = handle;
  558. return 0;
  559. }
  560. int
  561. i915_gem_dumb_create(struct drm_file *file,
  562. struct drm_device *dev,
  563. struct drm_mode_create_dumb *args)
  564. {
  565. /* have to work out size/pitch and return them */
  566. args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
  567. args->size = args->pitch * args->height;
  568. return i915_gem_create(file, to_i915(dev),
  569. args->size, &args->handle);
  570. }
  571. /**
  572. * Creates a new mm object and returns a handle to it.
  573. * @dev: drm device pointer
  574. * @data: ioctl data blob
  575. * @file: drm file pointer
  576. */
  577. int
  578. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  579. struct drm_file *file)
  580. {
  581. struct drm_i915_private *dev_priv = to_i915(dev);
  582. struct drm_i915_gem_create *args = data;
  583. i915_gem_flush_free_objects(dev_priv);
  584. return i915_gem_create(file, dev_priv,
  585. args->size, &args->handle);
  586. }
  587. static inline enum fb_op_origin
  588. fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
  589. {
  590. return (domain == I915_GEM_DOMAIN_GTT ?
  591. obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
  592. }
  593. static void
  594. flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
  595. {
  596. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  597. if (!(obj->base.write_domain & flush_domains))
  598. return;
  599. /* No actual flushing is required for the GTT write domain. Writes
  600. * to it "immediately" go to main memory as far as we know, so there's
  601. * no chipset flush. It also doesn't land in render cache.
  602. *
  603. * However, we do have to enforce the order so that all writes through
  604. * the GTT land before any writes to the device, such as updates to
  605. * the GATT itself.
  606. *
  607. * We also have to wait a bit for the writes to land from the GTT.
  608. * An uncached read (i.e. mmio) seems to be ideal for the round-trip
  609. * timing. This issue has only been observed when switching quickly
  610. * between GTT writes and CPU reads from inside the kernel on recent hw,
  611. * and it appears to only affect discrete GTT blocks (i.e. on LLC
  612. * system agents we cannot reproduce this behaviour).
  613. */
  614. wmb();
  615. switch (obj->base.write_domain) {
  616. case I915_GEM_DOMAIN_GTT:
  617. if (INTEL_GEN(dev_priv) >= 6 && !HAS_LLC(dev_priv)) {
  618. if (intel_runtime_pm_get_if_in_use(dev_priv)) {
  619. spin_lock_irq(&dev_priv->uncore.lock);
  620. POSTING_READ_FW(RING_ACTHD(dev_priv->engine[RCS]->mmio_base));
  621. spin_unlock_irq(&dev_priv->uncore.lock);
  622. intel_runtime_pm_put(dev_priv);
  623. }
  624. }
  625. intel_fb_obj_flush(obj,
  626. fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
  627. break;
  628. case I915_GEM_DOMAIN_CPU:
  629. i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
  630. break;
  631. }
  632. obj->base.write_domain = 0;
  633. }
  634. static inline int
  635. __copy_to_user_swizzled(char __user *cpu_vaddr,
  636. const char *gpu_vaddr, int gpu_offset,
  637. int length)
  638. {
  639. int ret, cpu_offset = 0;
  640. while (length > 0) {
  641. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  642. int this_length = min(cacheline_end - gpu_offset, length);
  643. int swizzled_gpu_offset = gpu_offset ^ 64;
  644. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  645. gpu_vaddr + swizzled_gpu_offset,
  646. this_length);
  647. if (ret)
  648. return ret + length;
  649. cpu_offset += this_length;
  650. gpu_offset += this_length;
  651. length -= this_length;
  652. }
  653. return 0;
  654. }
  655. static inline int
  656. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  657. const char __user *cpu_vaddr,
  658. int length)
  659. {
  660. int ret, cpu_offset = 0;
  661. while (length > 0) {
  662. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  663. int this_length = min(cacheline_end - gpu_offset, length);
  664. int swizzled_gpu_offset = gpu_offset ^ 64;
  665. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  666. cpu_vaddr + cpu_offset,
  667. this_length);
  668. if (ret)
  669. return ret + length;
  670. cpu_offset += this_length;
  671. gpu_offset += this_length;
  672. length -= this_length;
  673. }
  674. return 0;
  675. }
  676. /*
  677. * Pins the specified object's pages and synchronizes the object with
  678. * GPU accesses. Sets needs_clflush to non-zero if the caller should
  679. * flush the object from the CPU cache.
  680. */
  681. int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
  682. unsigned int *needs_clflush)
  683. {
  684. int ret;
  685. lockdep_assert_held(&obj->base.dev->struct_mutex);
  686. *needs_clflush = 0;
  687. if (!i915_gem_object_has_struct_page(obj))
  688. return -ENODEV;
  689. ret = i915_gem_object_wait(obj,
  690. I915_WAIT_INTERRUPTIBLE |
  691. I915_WAIT_LOCKED,
  692. MAX_SCHEDULE_TIMEOUT,
  693. NULL);
  694. if (ret)
  695. return ret;
  696. ret = i915_gem_object_pin_pages(obj);
  697. if (ret)
  698. return ret;
  699. if (i915_gem_object_is_coherent(obj) ||
  700. !static_cpu_has(X86_FEATURE_CLFLUSH)) {
  701. ret = i915_gem_object_set_to_cpu_domain(obj, false);
  702. if (ret)
  703. goto err_unpin;
  704. else
  705. goto out;
  706. }
  707. flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
  708. /* If we're not in the cpu read domain, set ourself into the gtt
  709. * read domain and manually flush cachelines (if required). This
  710. * optimizes for the case when the gpu will dirty the data
  711. * anyway again before the next pread happens.
  712. */
  713. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
  714. *needs_clflush = CLFLUSH_BEFORE;
  715. out:
  716. /* return with the pages pinned */
  717. return 0;
  718. err_unpin:
  719. i915_gem_object_unpin_pages(obj);
  720. return ret;
  721. }
  722. int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
  723. unsigned int *needs_clflush)
  724. {
  725. int ret;
  726. lockdep_assert_held(&obj->base.dev->struct_mutex);
  727. *needs_clflush = 0;
  728. if (!i915_gem_object_has_struct_page(obj))
  729. return -ENODEV;
  730. ret = i915_gem_object_wait(obj,
  731. I915_WAIT_INTERRUPTIBLE |
  732. I915_WAIT_LOCKED |
  733. I915_WAIT_ALL,
  734. MAX_SCHEDULE_TIMEOUT,
  735. NULL);
  736. if (ret)
  737. return ret;
  738. ret = i915_gem_object_pin_pages(obj);
  739. if (ret)
  740. return ret;
  741. if (i915_gem_object_is_coherent(obj) ||
  742. !static_cpu_has(X86_FEATURE_CLFLUSH)) {
  743. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  744. if (ret)
  745. goto err_unpin;
  746. else
  747. goto out;
  748. }
  749. flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
  750. /* If we're not in the cpu write domain, set ourself into the
  751. * gtt write domain and manually flush cachelines (as required).
  752. * This optimizes for the case when the gpu will use the data
  753. * right away and we therefore have to clflush anyway.
  754. */
  755. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  756. *needs_clflush |= CLFLUSH_AFTER;
  757. /* Same trick applies to invalidate partially written cachelines read
  758. * before writing.
  759. */
  760. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
  761. *needs_clflush |= CLFLUSH_BEFORE;
  762. out:
  763. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  764. obj->mm.dirty = true;
  765. /* return with the pages pinned */
  766. return 0;
  767. err_unpin:
  768. i915_gem_object_unpin_pages(obj);
  769. return ret;
  770. }
  771. static void
  772. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  773. bool swizzled)
  774. {
  775. if (unlikely(swizzled)) {
  776. unsigned long start = (unsigned long) addr;
  777. unsigned long end = (unsigned long) addr + length;
  778. /* For swizzling simply ensure that we always flush both
  779. * channels. Lame, but simple and it works. Swizzled
  780. * pwrite/pread is far from a hotpath - current userspace
  781. * doesn't use it at all. */
  782. start = round_down(start, 128);
  783. end = round_up(end, 128);
  784. drm_clflush_virt_range((void *)start, end - start);
  785. } else {
  786. drm_clflush_virt_range(addr, length);
  787. }
  788. }
  789. /* Only difference to the fast-path function is that this can handle bit17
  790. * and uses non-atomic copy and kmap functions. */
  791. static int
  792. shmem_pread_slow(struct page *page, int offset, int length,
  793. char __user *user_data,
  794. bool page_do_bit17_swizzling, bool needs_clflush)
  795. {
  796. char *vaddr;
  797. int ret;
  798. vaddr = kmap(page);
  799. if (needs_clflush)
  800. shmem_clflush_swizzled_range(vaddr + offset, length,
  801. page_do_bit17_swizzling);
  802. if (page_do_bit17_swizzling)
  803. ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
  804. else
  805. ret = __copy_to_user(user_data, vaddr + offset, length);
  806. kunmap(page);
  807. return ret ? - EFAULT : 0;
  808. }
  809. static int
  810. shmem_pread(struct page *page, int offset, int length, char __user *user_data,
  811. bool page_do_bit17_swizzling, bool needs_clflush)
  812. {
  813. int ret;
  814. ret = -ENODEV;
  815. if (!page_do_bit17_swizzling) {
  816. char *vaddr = kmap_atomic(page);
  817. if (needs_clflush)
  818. drm_clflush_virt_range(vaddr + offset, length);
  819. ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
  820. kunmap_atomic(vaddr);
  821. }
  822. if (ret == 0)
  823. return 0;
  824. return shmem_pread_slow(page, offset, length, user_data,
  825. page_do_bit17_swizzling, needs_clflush);
  826. }
  827. static int
  828. i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
  829. struct drm_i915_gem_pread *args)
  830. {
  831. char __user *user_data;
  832. u64 remain;
  833. unsigned int obj_do_bit17_swizzling;
  834. unsigned int needs_clflush;
  835. unsigned int idx, offset;
  836. int ret;
  837. obj_do_bit17_swizzling = 0;
  838. if (i915_gem_object_needs_bit17_swizzle(obj))
  839. obj_do_bit17_swizzling = BIT(17);
  840. ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
  841. if (ret)
  842. return ret;
  843. ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
  844. mutex_unlock(&obj->base.dev->struct_mutex);
  845. if (ret)
  846. return ret;
  847. remain = args->size;
  848. user_data = u64_to_user_ptr(args->data_ptr);
  849. offset = offset_in_page(args->offset);
  850. for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
  851. struct page *page = i915_gem_object_get_page(obj, idx);
  852. int length;
  853. length = remain;
  854. if (offset + length > PAGE_SIZE)
  855. length = PAGE_SIZE - offset;
  856. ret = shmem_pread(page, offset, length, user_data,
  857. page_to_phys(page) & obj_do_bit17_swizzling,
  858. needs_clflush);
  859. if (ret)
  860. break;
  861. remain -= length;
  862. user_data += length;
  863. offset = 0;
  864. }
  865. i915_gem_obj_finish_shmem_access(obj);
  866. return ret;
  867. }
  868. static inline bool
  869. gtt_user_read(struct io_mapping *mapping,
  870. loff_t base, int offset,
  871. char __user *user_data, int length)
  872. {
  873. void *vaddr;
  874. unsigned long unwritten;
  875. /* We can use the cpu mem copy function because this is X86. */
  876. vaddr = (void __force *)io_mapping_map_atomic_wc(mapping, base);
  877. unwritten = __copy_to_user_inatomic(user_data, vaddr + offset, length);
  878. io_mapping_unmap_atomic(vaddr);
  879. if (unwritten) {
  880. vaddr = (void __force *)
  881. io_mapping_map_wc(mapping, base, PAGE_SIZE);
  882. unwritten = copy_to_user(user_data, vaddr + offset, length);
  883. io_mapping_unmap(vaddr);
  884. }
  885. return unwritten;
  886. }
  887. static int
  888. i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
  889. const struct drm_i915_gem_pread *args)
  890. {
  891. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  892. struct i915_ggtt *ggtt = &i915->ggtt;
  893. struct drm_mm_node node;
  894. struct i915_vma *vma;
  895. void __user *user_data;
  896. u64 remain, offset;
  897. int ret;
  898. ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
  899. if (ret)
  900. return ret;
  901. intel_runtime_pm_get(i915);
  902. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  903. PIN_MAPPABLE | PIN_NONBLOCK);
  904. if (!IS_ERR(vma)) {
  905. node.start = i915_ggtt_offset(vma);
  906. node.allocated = false;
  907. ret = i915_vma_put_fence(vma);
  908. if (ret) {
  909. i915_vma_unpin(vma);
  910. vma = ERR_PTR(ret);
  911. }
  912. }
  913. if (IS_ERR(vma)) {
  914. ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
  915. if (ret)
  916. goto out_unlock;
  917. GEM_BUG_ON(!node.allocated);
  918. }
  919. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  920. if (ret)
  921. goto out_unpin;
  922. mutex_unlock(&i915->drm.struct_mutex);
  923. user_data = u64_to_user_ptr(args->data_ptr);
  924. remain = args->size;
  925. offset = args->offset;
  926. while (remain > 0) {
  927. /* Operation in this page
  928. *
  929. * page_base = page offset within aperture
  930. * page_offset = offset within page
  931. * page_length = bytes to copy for this page
  932. */
  933. u32 page_base = node.start;
  934. unsigned page_offset = offset_in_page(offset);
  935. unsigned page_length = PAGE_SIZE - page_offset;
  936. page_length = remain < page_length ? remain : page_length;
  937. if (node.allocated) {
  938. wmb();
  939. ggtt->base.insert_page(&ggtt->base,
  940. i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
  941. node.start, I915_CACHE_NONE, 0);
  942. wmb();
  943. } else {
  944. page_base += offset & PAGE_MASK;
  945. }
  946. if (gtt_user_read(&ggtt->mappable, page_base, page_offset,
  947. user_data, page_length)) {
  948. ret = -EFAULT;
  949. break;
  950. }
  951. remain -= page_length;
  952. user_data += page_length;
  953. offset += page_length;
  954. }
  955. mutex_lock(&i915->drm.struct_mutex);
  956. out_unpin:
  957. if (node.allocated) {
  958. wmb();
  959. ggtt->base.clear_range(&ggtt->base,
  960. node.start, node.size);
  961. remove_mappable_node(&node);
  962. } else {
  963. i915_vma_unpin(vma);
  964. }
  965. out_unlock:
  966. intel_runtime_pm_put(i915);
  967. mutex_unlock(&i915->drm.struct_mutex);
  968. return ret;
  969. }
  970. /**
  971. * Reads data from the object referenced by handle.
  972. * @dev: drm device pointer
  973. * @data: ioctl data blob
  974. * @file: drm file pointer
  975. *
  976. * On error, the contents of *data are undefined.
  977. */
  978. int
  979. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  980. struct drm_file *file)
  981. {
  982. struct drm_i915_gem_pread *args = data;
  983. struct drm_i915_gem_object *obj;
  984. int ret;
  985. if (args->size == 0)
  986. return 0;
  987. if (!access_ok(VERIFY_WRITE,
  988. u64_to_user_ptr(args->data_ptr),
  989. args->size))
  990. return -EFAULT;
  991. obj = i915_gem_object_lookup(file, args->handle);
  992. if (!obj)
  993. return -ENOENT;
  994. /* Bounds check source. */
  995. if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
  996. ret = -EINVAL;
  997. goto out;
  998. }
  999. trace_i915_gem_object_pread(obj, args->offset, args->size);
  1000. ret = i915_gem_object_wait(obj,
  1001. I915_WAIT_INTERRUPTIBLE,
  1002. MAX_SCHEDULE_TIMEOUT,
  1003. to_rps_client(file));
  1004. if (ret)
  1005. goto out;
  1006. ret = i915_gem_object_pin_pages(obj);
  1007. if (ret)
  1008. goto out;
  1009. ret = i915_gem_shmem_pread(obj, args);
  1010. if (ret == -EFAULT || ret == -ENODEV)
  1011. ret = i915_gem_gtt_pread(obj, args);
  1012. i915_gem_object_unpin_pages(obj);
  1013. out:
  1014. i915_gem_object_put(obj);
  1015. return ret;
  1016. }
  1017. /* This is the fast write path which cannot handle
  1018. * page faults in the source data
  1019. */
  1020. static inline bool
  1021. ggtt_write(struct io_mapping *mapping,
  1022. loff_t base, int offset,
  1023. char __user *user_data, int length)
  1024. {
  1025. void *vaddr;
  1026. unsigned long unwritten;
  1027. /* We can use the cpu mem copy function because this is X86. */
  1028. vaddr = (void __force *)io_mapping_map_atomic_wc(mapping, base);
  1029. unwritten = __copy_from_user_inatomic_nocache(vaddr + offset,
  1030. user_data, length);
  1031. io_mapping_unmap_atomic(vaddr);
  1032. if (unwritten) {
  1033. vaddr = (void __force *)
  1034. io_mapping_map_wc(mapping, base, PAGE_SIZE);
  1035. unwritten = copy_from_user(vaddr + offset, user_data, length);
  1036. io_mapping_unmap(vaddr);
  1037. }
  1038. return unwritten;
  1039. }
  1040. /**
  1041. * This is the fast pwrite path, where we copy the data directly from the
  1042. * user into the GTT, uncached.
  1043. * @obj: i915 GEM object
  1044. * @args: pwrite arguments structure
  1045. */
  1046. static int
  1047. i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
  1048. const struct drm_i915_gem_pwrite *args)
  1049. {
  1050. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  1051. struct i915_ggtt *ggtt = &i915->ggtt;
  1052. struct drm_mm_node node;
  1053. struct i915_vma *vma;
  1054. u64 remain, offset;
  1055. void __user *user_data;
  1056. int ret;
  1057. ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
  1058. if (ret)
  1059. return ret;
  1060. intel_runtime_pm_get(i915);
  1061. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  1062. PIN_MAPPABLE | PIN_NONBLOCK);
  1063. if (!IS_ERR(vma)) {
  1064. node.start = i915_ggtt_offset(vma);
  1065. node.allocated = false;
  1066. ret = i915_vma_put_fence(vma);
  1067. if (ret) {
  1068. i915_vma_unpin(vma);
  1069. vma = ERR_PTR(ret);
  1070. }
  1071. }
  1072. if (IS_ERR(vma)) {
  1073. ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
  1074. if (ret)
  1075. goto out_unlock;
  1076. GEM_BUG_ON(!node.allocated);
  1077. }
  1078. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  1079. if (ret)
  1080. goto out_unpin;
  1081. mutex_unlock(&i915->drm.struct_mutex);
  1082. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  1083. user_data = u64_to_user_ptr(args->data_ptr);
  1084. offset = args->offset;
  1085. remain = args->size;
  1086. while (remain) {
  1087. /* Operation in this page
  1088. *
  1089. * page_base = page offset within aperture
  1090. * page_offset = offset within page
  1091. * page_length = bytes to copy for this page
  1092. */
  1093. u32 page_base = node.start;
  1094. unsigned int page_offset = offset_in_page(offset);
  1095. unsigned int page_length = PAGE_SIZE - page_offset;
  1096. page_length = remain < page_length ? remain : page_length;
  1097. if (node.allocated) {
  1098. wmb(); /* flush the write before we modify the GGTT */
  1099. ggtt->base.insert_page(&ggtt->base,
  1100. i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
  1101. node.start, I915_CACHE_NONE, 0);
  1102. wmb(); /* flush modifications to the GGTT (insert_page) */
  1103. } else {
  1104. page_base += offset & PAGE_MASK;
  1105. }
  1106. /* If we get a fault while copying data, then (presumably) our
  1107. * source page isn't available. Return the error and we'll
  1108. * retry in the slow path.
  1109. * If the object is non-shmem backed, we retry again with the
  1110. * path that handles page fault.
  1111. */
  1112. if (ggtt_write(&ggtt->mappable, page_base, page_offset,
  1113. user_data, page_length)) {
  1114. ret = -EFAULT;
  1115. break;
  1116. }
  1117. remain -= page_length;
  1118. user_data += page_length;
  1119. offset += page_length;
  1120. }
  1121. intel_fb_obj_flush(obj, ORIGIN_CPU);
  1122. mutex_lock(&i915->drm.struct_mutex);
  1123. out_unpin:
  1124. if (node.allocated) {
  1125. wmb();
  1126. ggtt->base.clear_range(&ggtt->base,
  1127. node.start, node.size);
  1128. remove_mappable_node(&node);
  1129. } else {
  1130. i915_vma_unpin(vma);
  1131. }
  1132. out_unlock:
  1133. intel_runtime_pm_put(i915);
  1134. mutex_unlock(&i915->drm.struct_mutex);
  1135. return ret;
  1136. }
  1137. static int
  1138. shmem_pwrite_slow(struct page *page, int offset, int length,
  1139. char __user *user_data,
  1140. bool page_do_bit17_swizzling,
  1141. bool needs_clflush_before,
  1142. bool needs_clflush_after)
  1143. {
  1144. char *vaddr;
  1145. int ret;
  1146. vaddr = kmap(page);
  1147. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  1148. shmem_clflush_swizzled_range(vaddr + offset, length,
  1149. page_do_bit17_swizzling);
  1150. if (page_do_bit17_swizzling)
  1151. ret = __copy_from_user_swizzled(vaddr, offset, user_data,
  1152. length);
  1153. else
  1154. ret = __copy_from_user(vaddr + offset, user_data, length);
  1155. if (needs_clflush_after)
  1156. shmem_clflush_swizzled_range(vaddr + offset, length,
  1157. page_do_bit17_swizzling);
  1158. kunmap(page);
  1159. return ret ? -EFAULT : 0;
  1160. }
  1161. /* Per-page copy function for the shmem pwrite fastpath.
  1162. * Flushes invalid cachelines before writing to the target if
  1163. * needs_clflush_before is set and flushes out any written cachelines after
  1164. * writing if needs_clflush is set.
  1165. */
  1166. static int
  1167. shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
  1168. bool page_do_bit17_swizzling,
  1169. bool needs_clflush_before,
  1170. bool needs_clflush_after)
  1171. {
  1172. int ret;
  1173. ret = -ENODEV;
  1174. if (!page_do_bit17_swizzling) {
  1175. char *vaddr = kmap_atomic(page);
  1176. if (needs_clflush_before)
  1177. drm_clflush_virt_range(vaddr + offset, len);
  1178. ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
  1179. if (needs_clflush_after)
  1180. drm_clflush_virt_range(vaddr + offset, len);
  1181. kunmap_atomic(vaddr);
  1182. }
  1183. if (ret == 0)
  1184. return ret;
  1185. return shmem_pwrite_slow(page, offset, len, user_data,
  1186. page_do_bit17_swizzling,
  1187. needs_clflush_before,
  1188. needs_clflush_after);
  1189. }
  1190. static int
  1191. i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
  1192. const struct drm_i915_gem_pwrite *args)
  1193. {
  1194. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  1195. void __user *user_data;
  1196. u64 remain;
  1197. unsigned int obj_do_bit17_swizzling;
  1198. unsigned int partial_cacheline_write;
  1199. unsigned int needs_clflush;
  1200. unsigned int offset, idx;
  1201. int ret;
  1202. ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
  1203. if (ret)
  1204. return ret;
  1205. ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
  1206. mutex_unlock(&i915->drm.struct_mutex);
  1207. if (ret)
  1208. return ret;
  1209. obj_do_bit17_swizzling = 0;
  1210. if (i915_gem_object_needs_bit17_swizzle(obj))
  1211. obj_do_bit17_swizzling = BIT(17);
  1212. /* If we don't overwrite a cacheline completely we need to be
  1213. * careful to have up-to-date data by first clflushing. Don't
  1214. * overcomplicate things and flush the entire patch.
  1215. */
  1216. partial_cacheline_write = 0;
  1217. if (needs_clflush & CLFLUSH_BEFORE)
  1218. partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
  1219. user_data = u64_to_user_ptr(args->data_ptr);
  1220. remain = args->size;
  1221. offset = offset_in_page(args->offset);
  1222. for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
  1223. struct page *page = i915_gem_object_get_page(obj, idx);
  1224. int length;
  1225. length = remain;
  1226. if (offset + length > PAGE_SIZE)
  1227. length = PAGE_SIZE - offset;
  1228. ret = shmem_pwrite(page, offset, length, user_data,
  1229. page_to_phys(page) & obj_do_bit17_swizzling,
  1230. (offset | length) & partial_cacheline_write,
  1231. needs_clflush & CLFLUSH_AFTER);
  1232. if (ret)
  1233. break;
  1234. remain -= length;
  1235. user_data += length;
  1236. offset = 0;
  1237. }
  1238. intel_fb_obj_flush(obj, ORIGIN_CPU);
  1239. i915_gem_obj_finish_shmem_access(obj);
  1240. return ret;
  1241. }
  1242. /**
  1243. * Writes data to the object referenced by handle.
  1244. * @dev: drm device
  1245. * @data: ioctl data blob
  1246. * @file: drm file
  1247. *
  1248. * On error, the contents of the buffer that were to be modified are undefined.
  1249. */
  1250. int
  1251. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  1252. struct drm_file *file)
  1253. {
  1254. struct drm_i915_gem_pwrite *args = data;
  1255. struct drm_i915_gem_object *obj;
  1256. int ret;
  1257. if (args->size == 0)
  1258. return 0;
  1259. if (!access_ok(VERIFY_READ,
  1260. u64_to_user_ptr(args->data_ptr),
  1261. args->size))
  1262. return -EFAULT;
  1263. obj = i915_gem_object_lookup(file, args->handle);
  1264. if (!obj)
  1265. return -ENOENT;
  1266. /* Bounds check destination. */
  1267. if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
  1268. ret = -EINVAL;
  1269. goto err;
  1270. }
  1271. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  1272. ret = -ENODEV;
  1273. if (obj->ops->pwrite)
  1274. ret = obj->ops->pwrite(obj, args);
  1275. if (ret != -ENODEV)
  1276. goto err;
  1277. ret = i915_gem_object_wait(obj,
  1278. I915_WAIT_INTERRUPTIBLE |
  1279. I915_WAIT_ALL,
  1280. MAX_SCHEDULE_TIMEOUT,
  1281. to_rps_client(file));
  1282. if (ret)
  1283. goto err;
  1284. ret = i915_gem_object_pin_pages(obj);
  1285. if (ret)
  1286. goto err;
  1287. ret = -EFAULT;
  1288. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  1289. * it would end up going through the fenced access, and we'll get
  1290. * different detiling behavior between reading and writing.
  1291. * pread/pwrite currently are reading and writing from the CPU
  1292. * perspective, requiring manual detiling by the client.
  1293. */
  1294. if (!i915_gem_object_has_struct_page(obj) ||
  1295. cpu_write_needs_clflush(obj))
  1296. /* Note that the gtt paths might fail with non-page-backed user
  1297. * pointers (e.g. gtt mappings when moving data between
  1298. * textures). Fallback to the shmem path in that case.
  1299. */
  1300. ret = i915_gem_gtt_pwrite_fast(obj, args);
  1301. if (ret == -EFAULT || ret == -ENOSPC) {
  1302. if (obj->phys_handle)
  1303. ret = i915_gem_phys_pwrite(obj, args, file);
  1304. else
  1305. ret = i915_gem_shmem_pwrite(obj, args);
  1306. }
  1307. i915_gem_object_unpin_pages(obj);
  1308. err:
  1309. i915_gem_object_put(obj);
  1310. return ret;
  1311. }
  1312. static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
  1313. {
  1314. struct drm_i915_private *i915;
  1315. struct list_head *list;
  1316. struct i915_vma *vma;
  1317. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  1318. if (!i915_vma_is_ggtt(vma))
  1319. break;
  1320. if (i915_vma_is_active(vma))
  1321. continue;
  1322. if (!drm_mm_node_allocated(&vma->node))
  1323. continue;
  1324. list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
  1325. }
  1326. i915 = to_i915(obj->base.dev);
  1327. list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
  1328. list_move_tail(&obj->global_link, list);
  1329. }
  1330. /**
  1331. * Called when user space prepares to use an object with the CPU, either
  1332. * through the mmap ioctl's mapping or a GTT mapping.
  1333. * @dev: drm device
  1334. * @data: ioctl data blob
  1335. * @file: drm file
  1336. */
  1337. int
  1338. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  1339. struct drm_file *file)
  1340. {
  1341. struct drm_i915_gem_set_domain *args = data;
  1342. struct drm_i915_gem_object *obj;
  1343. uint32_t read_domains = args->read_domains;
  1344. uint32_t write_domain = args->write_domain;
  1345. int err;
  1346. /* Only handle setting domains to types used by the CPU. */
  1347. if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
  1348. return -EINVAL;
  1349. /* Having something in the write domain implies it's in the read
  1350. * domain, and only that read domain. Enforce that in the request.
  1351. */
  1352. if (write_domain != 0 && read_domains != write_domain)
  1353. return -EINVAL;
  1354. obj = i915_gem_object_lookup(file, args->handle);
  1355. if (!obj)
  1356. return -ENOENT;
  1357. /* Try to flush the object off the GPU without holding the lock.
  1358. * We will repeat the flush holding the lock in the normal manner
  1359. * to catch cases where we are gazumped.
  1360. */
  1361. err = i915_gem_object_wait(obj,
  1362. I915_WAIT_INTERRUPTIBLE |
  1363. (write_domain ? I915_WAIT_ALL : 0),
  1364. MAX_SCHEDULE_TIMEOUT,
  1365. to_rps_client(file));
  1366. if (err)
  1367. goto out;
  1368. /* Flush and acquire obj->pages so that we are coherent through
  1369. * direct access in memory with previous cached writes through
  1370. * shmemfs and that our cache domain tracking remains valid.
  1371. * For example, if the obj->filp was moved to swap without us
  1372. * being notified and releasing the pages, we would mistakenly
  1373. * continue to assume that the obj remained out of the CPU cached
  1374. * domain.
  1375. */
  1376. err = i915_gem_object_pin_pages(obj);
  1377. if (err)
  1378. goto out;
  1379. err = i915_mutex_lock_interruptible(dev);
  1380. if (err)
  1381. goto out_unpin;
  1382. if (read_domains & I915_GEM_DOMAIN_WC)
  1383. err = i915_gem_object_set_to_wc_domain(obj, write_domain);
  1384. else if (read_domains & I915_GEM_DOMAIN_GTT)
  1385. err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
  1386. else
  1387. err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
  1388. /* And bump the LRU for this access */
  1389. i915_gem_object_bump_inactive_ggtt(obj);
  1390. mutex_unlock(&dev->struct_mutex);
  1391. if (write_domain != 0)
  1392. intel_fb_obj_invalidate(obj,
  1393. fb_write_origin(obj, write_domain));
  1394. out_unpin:
  1395. i915_gem_object_unpin_pages(obj);
  1396. out:
  1397. i915_gem_object_put(obj);
  1398. return err;
  1399. }
  1400. /**
  1401. * Called when user space has done writes to this buffer
  1402. * @dev: drm device
  1403. * @data: ioctl data blob
  1404. * @file: drm file
  1405. */
  1406. int
  1407. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1408. struct drm_file *file)
  1409. {
  1410. struct drm_i915_gem_sw_finish *args = data;
  1411. struct drm_i915_gem_object *obj;
  1412. obj = i915_gem_object_lookup(file, args->handle);
  1413. if (!obj)
  1414. return -ENOENT;
  1415. /* Pinned buffers may be scanout, so flush the cache */
  1416. i915_gem_object_flush_if_display(obj);
  1417. i915_gem_object_put(obj);
  1418. return 0;
  1419. }
  1420. /**
  1421. * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
  1422. * it is mapped to.
  1423. * @dev: drm device
  1424. * @data: ioctl data blob
  1425. * @file: drm file
  1426. *
  1427. * While the mapping holds a reference on the contents of the object, it doesn't
  1428. * imply a ref on the object itself.
  1429. *
  1430. * IMPORTANT:
  1431. *
  1432. * DRM driver writers who look a this function as an example for how to do GEM
  1433. * mmap support, please don't implement mmap support like here. The modern way
  1434. * to implement DRM mmap support is with an mmap offset ioctl (like
  1435. * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
  1436. * That way debug tooling like valgrind will understand what's going on, hiding
  1437. * the mmap call in a driver private ioctl will break that. The i915 driver only
  1438. * does cpu mmaps this way because we didn't know better.
  1439. */
  1440. int
  1441. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1442. struct drm_file *file)
  1443. {
  1444. struct drm_i915_gem_mmap *args = data;
  1445. struct drm_i915_gem_object *obj;
  1446. unsigned long addr;
  1447. if (args->flags & ~(I915_MMAP_WC))
  1448. return -EINVAL;
  1449. if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
  1450. return -ENODEV;
  1451. obj = i915_gem_object_lookup(file, args->handle);
  1452. if (!obj)
  1453. return -ENOENT;
  1454. /* prime objects have no backing filp to GEM mmap
  1455. * pages from.
  1456. */
  1457. if (!obj->base.filp) {
  1458. i915_gem_object_put(obj);
  1459. return -EINVAL;
  1460. }
  1461. addr = vm_mmap(obj->base.filp, 0, args->size,
  1462. PROT_READ | PROT_WRITE, MAP_SHARED,
  1463. args->offset);
  1464. if (args->flags & I915_MMAP_WC) {
  1465. struct mm_struct *mm = current->mm;
  1466. struct vm_area_struct *vma;
  1467. if (down_write_killable(&mm->mmap_sem)) {
  1468. i915_gem_object_put(obj);
  1469. return -EINTR;
  1470. }
  1471. vma = find_vma(mm, addr);
  1472. if (vma)
  1473. vma->vm_page_prot =
  1474. pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  1475. else
  1476. addr = -ENOMEM;
  1477. up_write(&mm->mmap_sem);
  1478. /* This may race, but that's ok, it only gets set */
  1479. WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
  1480. }
  1481. i915_gem_object_put(obj);
  1482. if (IS_ERR((void *)addr))
  1483. return addr;
  1484. args->addr_ptr = (uint64_t) addr;
  1485. return 0;
  1486. }
  1487. static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
  1488. {
  1489. return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
  1490. }
  1491. /**
  1492. * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
  1493. *
  1494. * A history of the GTT mmap interface:
  1495. *
  1496. * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
  1497. * aligned and suitable for fencing, and still fit into the available
  1498. * mappable space left by the pinned display objects. A classic problem
  1499. * we called the page-fault-of-doom where we would ping-pong between
  1500. * two objects that could not fit inside the GTT and so the memcpy
  1501. * would page one object in at the expense of the other between every
  1502. * single byte.
  1503. *
  1504. * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
  1505. * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
  1506. * object is too large for the available space (or simply too large
  1507. * for the mappable aperture!), a view is created instead and faulted
  1508. * into userspace. (This view is aligned and sized appropriately for
  1509. * fenced access.)
  1510. *
  1511. * 2 - Recognise WC as a separate cache domain so that we can flush the
  1512. * delayed writes via GTT before performing direct access via WC.
  1513. *
  1514. * Restrictions:
  1515. *
  1516. * * snoopable objects cannot be accessed via the GTT. It can cause machine
  1517. * hangs on some architectures, corruption on others. An attempt to service
  1518. * a GTT page fault from a snoopable object will generate a SIGBUS.
  1519. *
  1520. * * the object must be able to fit into RAM (physical memory, though no
  1521. * limited to the mappable aperture).
  1522. *
  1523. *
  1524. * Caveats:
  1525. *
  1526. * * a new GTT page fault will synchronize rendering from the GPU and flush
  1527. * all data to system memory. Subsequent access will not be synchronized.
  1528. *
  1529. * * all mappings are revoked on runtime device suspend.
  1530. *
  1531. * * there are only 8, 16 or 32 fence registers to share between all users
  1532. * (older machines require fence register for display and blitter access
  1533. * as well). Contention of the fence registers will cause the previous users
  1534. * to be unmapped and any new access will generate new page faults.
  1535. *
  1536. * * running out of memory while servicing a fault may generate a SIGBUS,
  1537. * rather than the expected SIGSEGV.
  1538. */
  1539. int i915_gem_mmap_gtt_version(void)
  1540. {
  1541. return 2;
  1542. }
  1543. static inline struct i915_ggtt_view
  1544. compute_partial_view(struct drm_i915_gem_object *obj,
  1545. pgoff_t page_offset,
  1546. unsigned int chunk)
  1547. {
  1548. struct i915_ggtt_view view;
  1549. if (i915_gem_object_is_tiled(obj))
  1550. chunk = roundup(chunk, tile_row_pages(obj));
  1551. view.type = I915_GGTT_VIEW_PARTIAL;
  1552. view.partial.offset = rounddown(page_offset, chunk);
  1553. view.partial.size =
  1554. min_t(unsigned int, chunk,
  1555. (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
  1556. /* If the partial covers the entire object, just create a normal VMA. */
  1557. if (chunk >= obj->base.size >> PAGE_SHIFT)
  1558. view.type = I915_GGTT_VIEW_NORMAL;
  1559. return view;
  1560. }
  1561. /**
  1562. * i915_gem_fault - fault a page into the GTT
  1563. * @vmf: fault info
  1564. *
  1565. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1566. * from userspace. The fault handler takes care of binding the object to
  1567. * the GTT (if needed), allocating and programming a fence register (again,
  1568. * only if needed based on whether the old reg is still valid or the object
  1569. * is tiled) and inserting a new PTE into the faulting process.
  1570. *
  1571. * Note that the faulting process may involve evicting existing objects
  1572. * from the GTT and/or fence registers to make room. So performance may
  1573. * suffer if the GTT working set is large or there are few fence registers
  1574. * left.
  1575. *
  1576. * The current feature set supported by i915_gem_fault() and thus GTT mmaps
  1577. * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
  1578. */
  1579. int i915_gem_fault(struct vm_fault *vmf)
  1580. {
  1581. #define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
  1582. struct vm_area_struct *area = vmf->vma;
  1583. struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
  1584. struct drm_device *dev = obj->base.dev;
  1585. struct drm_i915_private *dev_priv = to_i915(dev);
  1586. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1587. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1588. struct i915_vma *vma;
  1589. pgoff_t page_offset;
  1590. unsigned int flags;
  1591. int ret;
  1592. /* We don't use vmf->pgoff since that has the fake offset */
  1593. page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
  1594. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1595. /* Try to flush the object off the GPU first without holding the lock.
  1596. * Upon acquiring the lock, we will perform our sanity checks and then
  1597. * repeat the flush holding the lock in the normal manner to catch cases
  1598. * where we are gazumped.
  1599. */
  1600. ret = i915_gem_object_wait(obj,
  1601. I915_WAIT_INTERRUPTIBLE,
  1602. MAX_SCHEDULE_TIMEOUT,
  1603. NULL);
  1604. if (ret)
  1605. goto err;
  1606. ret = i915_gem_object_pin_pages(obj);
  1607. if (ret)
  1608. goto err;
  1609. intel_runtime_pm_get(dev_priv);
  1610. ret = i915_mutex_lock_interruptible(dev);
  1611. if (ret)
  1612. goto err_rpm;
  1613. /* Access to snoopable pages through the GTT is incoherent. */
  1614. if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
  1615. ret = -EFAULT;
  1616. goto err_unlock;
  1617. }
  1618. /* If the object is smaller than a couple of partial vma, it is
  1619. * not worth only creating a single partial vma - we may as well
  1620. * clear enough space for the full object.
  1621. */
  1622. flags = PIN_MAPPABLE;
  1623. if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
  1624. flags |= PIN_NONBLOCK | PIN_NONFAULT;
  1625. /* Now pin it into the GTT as needed */
  1626. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
  1627. if (IS_ERR(vma)) {
  1628. /* Use a partial view if it is bigger than available space */
  1629. struct i915_ggtt_view view =
  1630. compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
  1631. /* Userspace is now writing through an untracked VMA, abandon
  1632. * all hope that the hardware is able to track future writes.
  1633. */
  1634. obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
  1635. vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
  1636. }
  1637. if (IS_ERR(vma)) {
  1638. ret = PTR_ERR(vma);
  1639. goto err_unlock;
  1640. }
  1641. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1642. if (ret)
  1643. goto err_unpin;
  1644. ret = i915_vma_get_fence(vma);
  1645. if (ret)
  1646. goto err_unpin;
  1647. /* Mark as being mmapped into userspace for later revocation */
  1648. assert_rpm_wakelock_held(dev_priv);
  1649. if (list_empty(&obj->userfault_link))
  1650. list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
  1651. /* Finally, remap it using the new GTT offset */
  1652. ret = remap_io_mapping(area,
  1653. area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
  1654. (ggtt->mappable_base + vma->node.start) >> PAGE_SHIFT,
  1655. min_t(u64, vma->size, area->vm_end - area->vm_start),
  1656. &ggtt->mappable);
  1657. err_unpin:
  1658. __i915_vma_unpin(vma);
  1659. err_unlock:
  1660. mutex_unlock(&dev->struct_mutex);
  1661. err_rpm:
  1662. intel_runtime_pm_put(dev_priv);
  1663. i915_gem_object_unpin_pages(obj);
  1664. err:
  1665. switch (ret) {
  1666. case -EIO:
  1667. /*
  1668. * We eat errors when the gpu is terminally wedged to avoid
  1669. * userspace unduly crashing (gl has no provisions for mmaps to
  1670. * fail). But any other -EIO isn't ours (e.g. swap in failure)
  1671. * and so needs to be reported.
  1672. */
  1673. if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
  1674. ret = VM_FAULT_SIGBUS;
  1675. break;
  1676. }
  1677. case -EAGAIN:
  1678. /*
  1679. * EAGAIN means the gpu is hung and we'll wait for the error
  1680. * handler to reset everything when re-faulting in
  1681. * i915_mutex_lock_interruptible.
  1682. */
  1683. case 0:
  1684. case -ERESTARTSYS:
  1685. case -EINTR:
  1686. case -EBUSY:
  1687. /*
  1688. * EBUSY is ok: this just means that another thread
  1689. * already did the job.
  1690. */
  1691. ret = VM_FAULT_NOPAGE;
  1692. break;
  1693. case -ENOMEM:
  1694. ret = VM_FAULT_OOM;
  1695. break;
  1696. case -ENOSPC:
  1697. case -EFAULT:
  1698. ret = VM_FAULT_SIGBUS;
  1699. break;
  1700. default:
  1701. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1702. ret = VM_FAULT_SIGBUS;
  1703. break;
  1704. }
  1705. return ret;
  1706. }
  1707. /**
  1708. * i915_gem_release_mmap - remove physical page mappings
  1709. * @obj: obj in question
  1710. *
  1711. * Preserve the reservation of the mmapping with the DRM core code, but
  1712. * relinquish ownership of the pages back to the system.
  1713. *
  1714. * It is vital that we remove the page mapping if we have mapped a tiled
  1715. * object through the GTT and then lose the fence register due to
  1716. * resource pressure. Similarly if the object has been moved out of the
  1717. * aperture, than pages mapped into userspace must be revoked. Removing the
  1718. * mapping will then trigger a page fault on the next user access, allowing
  1719. * fixup by i915_gem_fault().
  1720. */
  1721. void
  1722. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1723. {
  1724. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  1725. /* Serialisation between user GTT access and our code depends upon
  1726. * revoking the CPU's PTE whilst the mutex is held. The next user
  1727. * pagefault then has to wait until we release the mutex.
  1728. *
  1729. * Note that RPM complicates somewhat by adding an additional
  1730. * requirement that operations to the GGTT be made holding the RPM
  1731. * wakeref.
  1732. */
  1733. lockdep_assert_held(&i915->drm.struct_mutex);
  1734. intel_runtime_pm_get(i915);
  1735. if (list_empty(&obj->userfault_link))
  1736. goto out;
  1737. list_del_init(&obj->userfault_link);
  1738. drm_vma_node_unmap(&obj->base.vma_node,
  1739. obj->base.dev->anon_inode->i_mapping);
  1740. /* Ensure that the CPU's PTE are revoked and there are not outstanding
  1741. * memory transactions from userspace before we return. The TLB
  1742. * flushing implied above by changing the PTE above *should* be
  1743. * sufficient, an extra barrier here just provides us with a bit
  1744. * of paranoid documentation about our requirement to serialise
  1745. * memory writes before touching registers / GSM.
  1746. */
  1747. wmb();
  1748. out:
  1749. intel_runtime_pm_put(i915);
  1750. }
  1751. void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
  1752. {
  1753. struct drm_i915_gem_object *obj, *on;
  1754. int i;
  1755. /*
  1756. * Only called during RPM suspend. All users of the userfault_list
  1757. * must be holding an RPM wakeref to ensure that this can not
  1758. * run concurrently with themselves (and use the struct_mutex for
  1759. * protection between themselves).
  1760. */
  1761. list_for_each_entry_safe(obj, on,
  1762. &dev_priv->mm.userfault_list, userfault_link) {
  1763. list_del_init(&obj->userfault_link);
  1764. drm_vma_node_unmap(&obj->base.vma_node,
  1765. obj->base.dev->anon_inode->i_mapping);
  1766. }
  1767. /* The fence will be lost when the device powers down. If any were
  1768. * in use by hardware (i.e. they are pinned), we should not be powering
  1769. * down! All other fences will be reacquired by the user upon waking.
  1770. */
  1771. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  1772. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  1773. /* Ideally we want to assert that the fence register is not
  1774. * live at this point (i.e. that no piece of code will be
  1775. * trying to write through fence + GTT, as that both violates
  1776. * our tracking of activity and associated locking/barriers,
  1777. * but also is illegal given that the hw is powered down).
  1778. *
  1779. * Previously we used reg->pin_count as a "liveness" indicator.
  1780. * That is not sufficient, and we need a more fine-grained
  1781. * tool if we want to have a sanity check here.
  1782. */
  1783. if (!reg->vma)
  1784. continue;
  1785. GEM_BUG_ON(!list_empty(&reg->vma->obj->userfault_link));
  1786. reg->dirty = true;
  1787. }
  1788. }
  1789. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1790. {
  1791. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  1792. int err;
  1793. err = drm_gem_create_mmap_offset(&obj->base);
  1794. if (likely(!err))
  1795. return 0;
  1796. /* Attempt to reap some mmap space from dead objects */
  1797. do {
  1798. err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
  1799. if (err)
  1800. break;
  1801. i915_gem_drain_freed_objects(dev_priv);
  1802. err = drm_gem_create_mmap_offset(&obj->base);
  1803. if (!err)
  1804. break;
  1805. } while (flush_delayed_work(&dev_priv->gt.retire_work));
  1806. return err;
  1807. }
  1808. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1809. {
  1810. drm_gem_free_mmap_offset(&obj->base);
  1811. }
  1812. int
  1813. i915_gem_mmap_gtt(struct drm_file *file,
  1814. struct drm_device *dev,
  1815. uint32_t handle,
  1816. uint64_t *offset)
  1817. {
  1818. struct drm_i915_gem_object *obj;
  1819. int ret;
  1820. obj = i915_gem_object_lookup(file, handle);
  1821. if (!obj)
  1822. return -ENOENT;
  1823. ret = i915_gem_object_create_mmap_offset(obj);
  1824. if (ret == 0)
  1825. *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
  1826. i915_gem_object_put(obj);
  1827. return ret;
  1828. }
  1829. /**
  1830. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1831. * @dev: DRM device
  1832. * @data: GTT mapping ioctl data
  1833. * @file: GEM object info
  1834. *
  1835. * Simply returns the fake offset to userspace so it can mmap it.
  1836. * The mmap call will end up in drm_gem_mmap(), which will set things
  1837. * up so we can get faults in the handler above.
  1838. *
  1839. * The fault handler will take care of binding the object into the GTT
  1840. * (since it may have been evicted to make room for something), allocating
  1841. * a fence register, and mapping the appropriate aperture address into
  1842. * userspace.
  1843. */
  1844. int
  1845. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1846. struct drm_file *file)
  1847. {
  1848. struct drm_i915_gem_mmap_gtt *args = data;
  1849. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1850. }
  1851. /* Immediately discard the backing storage */
  1852. static void
  1853. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1854. {
  1855. i915_gem_object_free_mmap_offset(obj);
  1856. if (obj->base.filp == NULL)
  1857. return;
  1858. /* Our goal here is to return as much of the memory as
  1859. * is possible back to the system as we are called from OOM.
  1860. * To do this we must instruct the shmfs to drop all of its
  1861. * backing pages, *now*.
  1862. */
  1863. shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
  1864. obj->mm.madv = __I915_MADV_PURGED;
  1865. obj->mm.pages = ERR_PTR(-EFAULT);
  1866. }
  1867. /* Try to discard unwanted pages */
  1868. void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
  1869. {
  1870. struct address_space *mapping;
  1871. lockdep_assert_held(&obj->mm.lock);
  1872. GEM_BUG_ON(obj->mm.pages);
  1873. switch (obj->mm.madv) {
  1874. case I915_MADV_DONTNEED:
  1875. i915_gem_object_truncate(obj);
  1876. case __I915_MADV_PURGED:
  1877. return;
  1878. }
  1879. if (obj->base.filp == NULL)
  1880. return;
  1881. mapping = obj->base.filp->f_mapping,
  1882. invalidate_mapping_pages(mapping, 0, (loff_t)-1);
  1883. }
  1884. static void
  1885. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
  1886. struct sg_table *pages)
  1887. {
  1888. struct sgt_iter sgt_iter;
  1889. struct page *page;
  1890. __i915_gem_object_release_shmem(obj, pages, true);
  1891. i915_gem_gtt_finish_pages(obj, pages);
  1892. if (i915_gem_object_needs_bit17_swizzle(obj))
  1893. i915_gem_object_save_bit_17_swizzle(obj, pages);
  1894. for_each_sgt_page(page, sgt_iter, pages) {
  1895. if (obj->mm.dirty)
  1896. set_page_dirty(page);
  1897. if (obj->mm.madv == I915_MADV_WILLNEED)
  1898. mark_page_accessed(page);
  1899. put_page(page);
  1900. }
  1901. obj->mm.dirty = false;
  1902. sg_free_table(pages);
  1903. kfree(pages);
  1904. }
  1905. static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
  1906. {
  1907. struct radix_tree_iter iter;
  1908. void **slot;
  1909. radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
  1910. radix_tree_delete(&obj->mm.get_page.radix, iter.index);
  1911. }
  1912. void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
  1913. enum i915_mm_subclass subclass)
  1914. {
  1915. struct sg_table *pages;
  1916. if (i915_gem_object_has_pinned_pages(obj))
  1917. return;
  1918. GEM_BUG_ON(obj->bind_count);
  1919. if (!READ_ONCE(obj->mm.pages))
  1920. return;
  1921. /* May be called by shrinker from within get_pages() (on another bo) */
  1922. mutex_lock_nested(&obj->mm.lock, subclass);
  1923. if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
  1924. goto unlock;
  1925. /* ->put_pages might need to allocate memory for the bit17 swizzle
  1926. * array, hence protect them from being reaped by removing them from gtt
  1927. * lists early. */
  1928. pages = fetch_and_zero(&obj->mm.pages);
  1929. GEM_BUG_ON(!pages);
  1930. if (obj->mm.mapping) {
  1931. void *ptr;
  1932. ptr = ptr_mask_bits(obj->mm.mapping);
  1933. if (is_vmalloc_addr(ptr))
  1934. vunmap(ptr);
  1935. else
  1936. kunmap(kmap_to_page(ptr));
  1937. obj->mm.mapping = NULL;
  1938. }
  1939. __i915_gem_object_reset_page_iter(obj);
  1940. if (!IS_ERR(pages))
  1941. obj->ops->put_pages(obj, pages);
  1942. unlock:
  1943. mutex_unlock(&obj->mm.lock);
  1944. }
  1945. static bool i915_sg_trim(struct sg_table *orig_st)
  1946. {
  1947. struct sg_table new_st;
  1948. struct scatterlist *sg, *new_sg;
  1949. unsigned int i;
  1950. if (orig_st->nents == orig_st->orig_nents)
  1951. return false;
  1952. if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
  1953. return false;
  1954. new_sg = new_st.sgl;
  1955. for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
  1956. sg_set_page(new_sg, sg_page(sg), sg->length, 0);
  1957. /* called before being DMA mapped, no need to copy sg->dma_* */
  1958. new_sg = sg_next(new_sg);
  1959. }
  1960. GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
  1961. sg_free_table(orig_st);
  1962. *orig_st = new_st;
  1963. return true;
  1964. }
  1965. static struct sg_table *
  1966. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  1967. {
  1968. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  1969. const unsigned long page_count = obj->base.size / PAGE_SIZE;
  1970. unsigned long i;
  1971. struct address_space *mapping;
  1972. struct sg_table *st;
  1973. struct scatterlist *sg;
  1974. struct sgt_iter sgt_iter;
  1975. struct page *page;
  1976. unsigned long last_pfn = 0; /* suppress gcc warning */
  1977. unsigned int max_segment;
  1978. int ret;
  1979. gfp_t gfp;
  1980. /* Assert that the object is not currently in any GPU domain. As it
  1981. * wasn't in the GTT, there shouldn't be any way it could have been in
  1982. * a GPU cache
  1983. */
  1984. GEM_BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  1985. GEM_BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  1986. max_segment = swiotlb_max_segment();
  1987. if (!max_segment)
  1988. max_segment = rounddown(UINT_MAX, PAGE_SIZE);
  1989. st = kmalloc(sizeof(*st), GFP_KERNEL);
  1990. if (st == NULL)
  1991. return ERR_PTR(-ENOMEM);
  1992. rebuild_st:
  1993. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  1994. kfree(st);
  1995. return ERR_PTR(-ENOMEM);
  1996. }
  1997. /* Get the list of pages out of our struct file. They'll be pinned
  1998. * at this point until we release them.
  1999. *
  2000. * Fail silently without starting the shrinker
  2001. */
  2002. mapping = obj->base.filp->f_mapping;
  2003. gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM));
  2004. gfp |= __GFP_NORETRY | __GFP_NOWARN;
  2005. sg = st->sgl;
  2006. st->nents = 0;
  2007. for (i = 0; i < page_count; i++) {
  2008. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  2009. if (unlikely(IS_ERR(page))) {
  2010. i915_gem_shrink(dev_priv,
  2011. page_count,
  2012. I915_SHRINK_BOUND |
  2013. I915_SHRINK_UNBOUND |
  2014. I915_SHRINK_PURGEABLE);
  2015. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  2016. }
  2017. if (unlikely(IS_ERR(page))) {
  2018. gfp_t reclaim;
  2019. /* We've tried hard to allocate the memory by reaping
  2020. * our own buffer, now let the real VM do its job and
  2021. * go down in flames if truly OOM.
  2022. *
  2023. * However, since graphics tend to be disposable,
  2024. * defer the oom here by reporting the ENOMEM back
  2025. * to userspace.
  2026. */
  2027. reclaim = mapping_gfp_mask(mapping);
  2028. reclaim |= __GFP_NORETRY; /* reclaim, but no oom */
  2029. page = shmem_read_mapping_page_gfp(mapping, i, reclaim);
  2030. if (IS_ERR(page)) {
  2031. ret = PTR_ERR(page);
  2032. goto err_sg;
  2033. }
  2034. }
  2035. if (!i ||
  2036. sg->length >= max_segment ||
  2037. page_to_pfn(page) != last_pfn + 1) {
  2038. if (i)
  2039. sg = sg_next(sg);
  2040. st->nents++;
  2041. sg_set_page(sg, page, PAGE_SIZE, 0);
  2042. } else {
  2043. sg->length += PAGE_SIZE;
  2044. }
  2045. last_pfn = page_to_pfn(page);
  2046. /* Check that the i965g/gm workaround works. */
  2047. WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
  2048. }
  2049. if (sg) /* loop terminated early; short sg table */
  2050. sg_mark_end(sg);
  2051. /* Trim unused sg entries to avoid wasting memory. */
  2052. i915_sg_trim(st);
  2053. ret = i915_gem_gtt_prepare_pages(obj, st);
  2054. if (ret) {
  2055. /* DMA remapping failed? One possible cause is that
  2056. * it could not reserve enough large entries, asking
  2057. * for PAGE_SIZE chunks instead may be helpful.
  2058. */
  2059. if (max_segment > PAGE_SIZE) {
  2060. for_each_sgt_page(page, sgt_iter, st)
  2061. put_page(page);
  2062. sg_free_table(st);
  2063. max_segment = PAGE_SIZE;
  2064. goto rebuild_st;
  2065. } else {
  2066. dev_warn(&dev_priv->drm.pdev->dev,
  2067. "Failed to DMA remap %lu pages\n",
  2068. page_count);
  2069. goto err_pages;
  2070. }
  2071. }
  2072. if (i915_gem_object_needs_bit17_swizzle(obj))
  2073. i915_gem_object_do_bit_17_swizzle(obj, st);
  2074. return st;
  2075. err_sg:
  2076. sg_mark_end(sg);
  2077. err_pages:
  2078. for_each_sgt_page(page, sgt_iter, st)
  2079. put_page(page);
  2080. sg_free_table(st);
  2081. kfree(st);
  2082. /* shmemfs first checks if there is enough memory to allocate the page
  2083. * and reports ENOSPC should there be insufficient, along with the usual
  2084. * ENOMEM for a genuine allocation failure.
  2085. *
  2086. * We use ENOSPC in our driver to mean that we have run out of aperture
  2087. * space and so want to translate the error from shmemfs back to our
  2088. * usual understanding of ENOMEM.
  2089. */
  2090. if (ret == -ENOSPC)
  2091. ret = -ENOMEM;
  2092. return ERR_PTR(ret);
  2093. }
  2094. void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
  2095. struct sg_table *pages)
  2096. {
  2097. lockdep_assert_held(&obj->mm.lock);
  2098. obj->mm.get_page.sg_pos = pages->sgl;
  2099. obj->mm.get_page.sg_idx = 0;
  2100. obj->mm.pages = pages;
  2101. if (i915_gem_object_is_tiled(obj) &&
  2102. to_i915(obj->base.dev)->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  2103. GEM_BUG_ON(obj->mm.quirked);
  2104. __i915_gem_object_pin_pages(obj);
  2105. obj->mm.quirked = true;
  2106. }
  2107. }
  2108. static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  2109. {
  2110. struct sg_table *pages;
  2111. GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
  2112. if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
  2113. DRM_DEBUG("Attempting to obtain a purgeable object\n");
  2114. return -EFAULT;
  2115. }
  2116. pages = obj->ops->get_pages(obj);
  2117. if (unlikely(IS_ERR(pages)))
  2118. return PTR_ERR(pages);
  2119. __i915_gem_object_set_pages(obj, pages);
  2120. return 0;
  2121. }
  2122. /* Ensure that the associated pages are gathered from the backing storage
  2123. * and pinned into our object. i915_gem_object_pin_pages() may be called
  2124. * multiple times before they are released by a single call to
  2125. * i915_gem_object_unpin_pages() - once the pages are no longer referenced
  2126. * either as a result of memory pressure (reaping pages under the shrinker)
  2127. * or as the object is itself released.
  2128. */
  2129. int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  2130. {
  2131. int err;
  2132. err = mutex_lock_interruptible(&obj->mm.lock);
  2133. if (err)
  2134. return err;
  2135. if (unlikely(IS_ERR_OR_NULL(obj->mm.pages))) {
  2136. err = ____i915_gem_object_get_pages(obj);
  2137. if (err)
  2138. goto unlock;
  2139. smp_mb__before_atomic();
  2140. }
  2141. atomic_inc(&obj->mm.pages_pin_count);
  2142. unlock:
  2143. mutex_unlock(&obj->mm.lock);
  2144. return err;
  2145. }
  2146. /* The 'mapping' part of i915_gem_object_pin_map() below */
  2147. static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
  2148. enum i915_map_type type)
  2149. {
  2150. unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
  2151. struct sg_table *sgt = obj->mm.pages;
  2152. struct sgt_iter sgt_iter;
  2153. struct page *page;
  2154. struct page *stack_pages[32];
  2155. struct page **pages = stack_pages;
  2156. unsigned long i = 0;
  2157. pgprot_t pgprot;
  2158. void *addr;
  2159. /* A single page can always be kmapped */
  2160. if (n_pages == 1 && type == I915_MAP_WB)
  2161. return kmap(sg_page(sgt->sgl));
  2162. if (n_pages > ARRAY_SIZE(stack_pages)) {
  2163. /* Too big for stack -- allocate temporary array instead */
  2164. pages = drm_malloc_gfp(n_pages, sizeof(*pages), GFP_TEMPORARY);
  2165. if (!pages)
  2166. return NULL;
  2167. }
  2168. for_each_sgt_page(page, sgt_iter, sgt)
  2169. pages[i++] = page;
  2170. /* Check that we have the expected number of pages */
  2171. GEM_BUG_ON(i != n_pages);
  2172. switch (type) {
  2173. case I915_MAP_WB:
  2174. pgprot = PAGE_KERNEL;
  2175. break;
  2176. case I915_MAP_WC:
  2177. pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
  2178. break;
  2179. }
  2180. addr = vmap(pages, n_pages, 0, pgprot);
  2181. if (pages != stack_pages)
  2182. drm_free_large(pages);
  2183. return addr;
  2184. }
  2185. /* get, pin, and map the pages of the object into kernel space */
  2186. void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
  2187. enum i915_map_type type)
  2188. {
  2189. enum i915_map_type has_type;
  2190. bool pinned;
  2191. void *ptr;
  2192. int ret;
  2193. GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
  2194. ret = mutex_lock_interruptible(&obj->mm.lock);
  2195. if (ret)
  2196. return ERR_PTR(ret);
  2197. pinned = true;
  2198. if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
  2199. if (unlikely(IS_ERR_OR_NULL(obj->mm.pages))) {
  2200. ret = ____i915_gem_object_get_pages(obj);
  2201. if (ret)
  2202. goto err_unlock;
  2203. smp_mb__before_atomic();
  2204. }
  2205. atomic_inc(&obj->mm.pages_pin_count);
  2206. pinned = false;
  2207. }
  2208. GEM_BUG_ON(!obj->mm.pages);
  2209. ptr = ptr_unpack_bits(obj->mm.mapping, has_type);
  2210. if (ptr && has_type != type) {
  2211. if (pinned) {
  2212. ret = -EBUSY;
  2213. goto err_unpin;
  2214. }
  2215. if (is_vmalloc_addr(ptr))
  2216. vunmap(ptr);
  2217. else
  2218. kunmap(kmap_to_page(ptr));
  2219. ptr = obj->mm.mapping = NULL;
  2220. }
  2221. if (!ptr) {
  2222. ptr = i915_gem_object_map(obj, type);
  2223. if (!ptr) {
  2224. ret = -ENOMEM;
  2225. goto err_unpin;
  2226. }
  2227. obj->mm.mapping = ptr_pack_bits(ptr, type);
  2228. }
  2229. out_unlock:
  2230. mutex_unlock(&obj->mm.lock);
  2231. return ptr;
  2232. err_unpin:
  2233. atomic_dec(&obj->mm.pages_pin_count);
  2234. err_unlock:
  2235. ptr = ERR_PTR(ret);
  2236. goto out_unlock;
  2237. }
  2238. static int
  2239. i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
  2240. const struct drm_i915_gem_pwrite *arg)
  2241. {
  2242. struct address_space *mapping = obj->base.filp->f_mapping;
  2243. char __user *user_data = u64_to_user_ptr(arg->data_ptr);
  2244. u64 remain, offset;
  2245. unsigned int pg;
  2246. /* Before we instantiate/pin the backing store for our use, we
  2247. * can prepopulate the shmemfs filp efficiently using a write into
  2248. * the pagecache. We avoid the penalty of instantiating all the
  2249. * pages, important if the user is just writing to a few and never
  2250. * uses the object on the GPU, and using a direct write into shmemfs
  2251. * allows it to avoid the cost of retrieving a page (either swapin
  2252. * or clearing-before-use) before it is overwritten.
  2253. */
  2254. if (READ_ONCE(obj->mm.pages))
  2255. return -ENODEV;
  2256. /* Before the pages are instantiated the object is treated as being
  2257. * in the CPU domain. The pages will be clflushed as required before
  2258. * use, and we can freely write into the pages directly. If userspace
  2259. * races pwrite with any other operation; corruption will ensue -
  2260. * that is userspace's prerogative!
  2261. */
  2262. remain = arg->size;
  2263. offset = arg->offset;
  2264. pg = offset_in_page(offset);
  2265. do {
  2266. unsigned int len, unwritten;
  2267. struct page *page;
  2268. void *data, *vaddr;
  2269. int err;
  2270. len = PAGE_SIZE - pg;
  2271. if (len > remain)
  2272. len = remain;
  2273. err = pagecache_write_begin(obj->base.filp, mapping,
  2274. offset, len, 0,
  2275. &page, &data);
  2276. if (err < 0)
  2277. return err;
  2278. vaddr = kmap(page);
  2279. unwritten = copy_from_user(vaddr + pg, user_data, len);
  2280. kunmap(page);
  2281. err = pagecache_write_end(obj->base.filp, mapping,
  2282. offset, len, len - unwritten,
  2283. page, data);
  2284. if (err < 0)
  2285. return err;
  2286. if (unwritten)
  2287. return -EFAULT;
  2288. remain -= len;
  2289. user_data += len;
  2290. offset += len;
  2291. pg = 0;
  2292. } while (remain);
  2293. return 0;
  2294. }
  2295. static bool ban_context(const struct i915_gem_context *ctx)
  2296. {
  2297. return (i915_gem_context_is_bannable(ctx) &&
  2298. ctx->ban_score >= CONTEXT_SCORE_BAN_THRESHOLD);
  2299. }
  2300. static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
  2301. {
  2302. ctx->guilty_count++;
  2303. ctx->ban_score += CONTEXT_SCORE_GUILTY;
  2304. if (ban_context(ctx))
  2305. i915_gem_context_set_banned(ctx);
  2306. DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
  2307. ctx->name, ctx->ban_score,
  2308. yesno(i915_gem_context_is_banned(ctx)));
  2309. if (!i915_gem_context_is_banned(ctx) || IS_ERR_OR_NULL(ctx->file_priv))
  2310. return;
  2311. ctx->file_priv->context_bans++;
  2312. DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
  2313. ctx->name, ctx->file_priv->context_bans);
  2314. }
  2315. static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
  2316. {
  2317. ctx->active_count++;
  2318. }
  2319. struct drm_i915_gem_request *
  2320. i915_gem_find_active_request(struct intel_engine_cs *engine)
  2321. {
  2322. struct drm_i915_gem_request *request, *active = NULL;
  2323. unsigned long flags;
  2324. /* We are called by the error capture and reset at a random
  2325. * point in time. In particular, note that neither is crucially
  2326. * ordered with an interrupt. After a hang, the GPU is dead and we
  2327. * assume that no more writes can happen (we waited long enough for
  2328. * all writes that were in transaction to be flushed) - adding an
  2329. * extra delay for a recent interrupt is pointless. Hence, we do
  2330. * not need an engine->irq_seqno_barrier() before the seqno reads.
  2331. */
  2332. spin_lock_irqsave(&engine->timeline->lock, flags);
  2333. list_for_each_entry(request, &engine->timeline->requests, link) {
  2334. if (__i915_gem_request_completed(request,
  2335. request->global_seqno))
  2336. continue;
  2337. GEM_BUG_ON(request->engine != engine);
  2338. GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
  2339. &request->fence.flags));
  2340. active = request;
  2341. break;
  2342. }
  2343. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  2344. return active;
  2345. }
  2346. static bool engine_stalled(struct intel_engine_cs *engine)
  2347. {
  2348. if (!engine->hangcheck.stalled)
  2349. return false;
  2350. /* Check for possible seqno movement after hang declaration */
  2351. if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) {
  2352. DRM_DEBUG_DRIVER("%s pardoned\n", engine->name);
  2353. return false;
  2354. }
  2355. return true;
  2356. }
  2357. int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
  2358. {
  2359. struct intel_engine_cs *engine;
  2360. enum intel_engine_id id;
  2361. int err = 0;
  2362. /* Ensure irq handler finishes, and not run again. */
  2363. for_each_engine(engine, dev_priv, id) {
  2364. struct drm_i915_gem_request *request;
  2365. /* Prevent the signaler thread from updating the request
  2366. * state (by calling dma_fence_signal) as we are processing
  2367. * the reset. The write from the GPU of the seqno is
  2368. * asynchronous and the signaler thread may see a different
  2369. * value to us and declare the request complete, even though
  2370. * the reset routine have picked that request as the active
  2371. * (incomplete) request. This conflict is not handled
  2372. * gracefully!
  2373. */
  2374. kthread_park(engine->breadcrumbs.signaler);
  2375. /* Prevent request submission to the hardware until we have
  2376. * completed the reset in i915_gem_reset_finish(). If a request
  2377. * is completed by one engine, it may then queue a request
  2378. * to a second via its engine->irq_tasklet *just* as we are
  2379. * calling engine->init_hw() and also writing the ELSP.
  2380. * Turning off the engine->irq_tasklet until the reset is over
  2381. * prevents the race.
  2382. */
  2383. tasklet_kill(&engine->irq_tasklet);
  2384. tasklet_disable(&engine->irq_tasklet);
  2385. if (engine->irq_seqno_barrier)
  2386. engine->irq_seqno_barrier(engine);
  2387. if (engine_stalled(engine)) {
  2388. request = i915_gem_find_active_request(engine);
  2389. if (request && request->fence.error == -EIO)
  2390. err = -EIO; /* Previous reset failed! */
  2391. }
  2392. }
  2393. i915_gem_revoke_fences(dev_priv);
  2394. return err;
  2395. }
  2396. static void skip_request(struct drm_i915_gem_request *request)
  2397. {
  2398. void *vaddr = request->ring->vaddr;
  2399. u32 head;
  2400. /* As this request likely depends on state from the lost
  2401. * context, clear out all the user operations leaving the
  2402. * breadcrumb at the end (so we get the fence notifications).
  2403. */
  2404. head = request->head;
  2405. if (request->postfix < head) {
  2406. memset(vaddr + head, 0, request->ring->size - head);
  2407. head = 0;
  2408. }
  2409. memset(vaddr + head, 0, request->postfix - head);
  2410. dma_fence_set_error(&request->fence, -EIO);
  2411. }
  2412. static void engine_skip_context(struct drm_i915_gem_request *request)
  2413. {
  2414. struct intel_engine_cs *engine = request->engine;
  2415. struct i915_gem_context *hung_ctx = request->ctx;
  2416. struct intel_timeline *timeline;
  2417. unsigned long flags;
  2418. timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
  2419. spin_lock_irqsave(&engine->timeline->lock, flags);
  2420. spin_lock(&timeline->lock);
  2421. list_for_each_entry_continue(request, &engine->timeline->requests, link)
  2422. if (request->ctx == hung_ctx)
  2423. skip_request(request);
  2424. list_for_each_entry(request, &timeline->requests, link)
  2425. skip_request(request);
  2426. spin_unlock(&timeline->lock);
  2427. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  2428. }
  2429. /* Returns true if the request was guilty of hang */
  2430. static bool i915_gem_reset_request(struct drm_i915_gem_request *request)
  2431. {
  2432. /* Read once and return the resolution */
  2433. const bool guilty = engine_stalled(request->engine);
  2434. /* The guilty request will get skipped on a hung engine.
  2435. *
  2436. * Users of client default contexts do not rely on logical
  2437. * state preserved between batches so it is safe to execute
  2438. * queued requests following the hang. Non default contexts
  2439. * rely on preserved state, so skipping a batch loses the
  2440. * evolution of the state and it needs to be considered corrupted.
  2441. * Executing more queued batches on top of corrupted state is
  2442. * risky. But we take the risk by trying to advance through
  2443. * the queued requests in order to make the client behaviour
  2444. * more predictable around resets, by not throwing away random
  2445. * amount of batches it has prepared for execution. Sophisticated
  2446. * clients can use gem_reset_stats_ioctl and dma fence status
  2447. * (exported via sync_file info ioctl on explicit fences) to observe
  2448. * when it loses the context state and should rebuild accordingly.
  2449. *
  2450. * The context ban, and ultimately the client ban, mechanism are safety
  2451. * valves if client submission ends up resulting in nothing more than
  2452. * subsequent hangs.
  2453. */
  2454. if (guilty) {
  2455. i915_gem_context_mark_guilty(request->ctx);
  2456. skip_request(request);
  2457. } else {
  2458. i915_gem_context_mark_innocent(request->ctx);
  2459. dma_fence_set_error(&request->fence, -EAGAIN);
  2460. }
  2461. return guilty;
  2462. }
  2463. static void i915_gem_reset_engine(struct intel_engine_cs *engine)
  2464. {
  2465. struct drm_i915_gem_request *request;
  2466. request = i915_gem_find_active_request(engine);
  2467. if (request && i915_gem_reset_request(request)) {
  2468. DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
  2469. engine->name, request->global_seqno);
  2470. /* If this context is now banned, skip all pending requests. */
  2471. if (i915_gem_context_is_banned(request->ctx))
  2472. engine_skip_context(request);
  2473. }
  2474. /* Setup the CS to resume from the breadcrumb of the hung request */
  2475. engine->reset_hw(engine, request);
  2476. }
  2477. void i915_gem_reset(struct drm_i915_private *dev_priv)
  2478. {
  2479. struct intel_engine_cs *engine;
  2480. enum intel_engine_id id;
  2481. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  2482. i915_gem_retire_requests(dev_priv);
  2483. for_each_engine(engine, dev_priv, id) {
  2484. struct i915_gem_context *ctx;
  2485. i915_gem_reset_engine(engine);
  2486. ctx = fetch_and_zero(&engine->last_retired_context);
  2487. if (ctx)
  2488. engine->context_unpin(engine, ctx);
  2489. }
  2490. i915_gem_restore_fences(dev_priv);
  2491. if (dev_priv->gt.awake) {
  2492. intel_sanitize_gt_powersave(dev_priv);
  2493. intel_enable_gt_powersave(dev_priv);
  2494. if (INTEL_GEN(dev_priv) >= 6)
  2495. gen6_rps_busy(dev_priv);
  2496. }
  2497. }
  2498. void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
  2499. {
  2500. struct intel_engine_cs *engine;
  2501. enum intel_engine_id id;
  2502. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  2503. for_each_engine(engine, dev_priv, id) {
  2504. tasklet_enable(&engine->irq_tasklet);
  2505. kthread_unpark(engine->breadcrumbs.signaler);
  2506. }
  2507. }
  2508. static void nop_submit_request(struct drm_i915_gem_request *request)
  2509. {
  2510. dma_fence_set_error(&request->fence, -EIO);
  2511. i915_gem_request_submit(request);
  2512. intel_engine_init_global_seqno(request->engine, request->global_seqno);
  2513. }
  2514. static void engine_set_wedged(struct intel_engine_cs *engine)
  2515. {
  2516. struct drm_i915_gem_request *request;
  2517. unsigned long flags;
  2518. /* We need to be sure that no thread is running the old callback as
  2519. * we install the nop handler (otherwise we would submit a request
  2520. * to hardware that will never complete). In order to prevent this
  2521. * race, we wait until the machine is idle before making the swap
  2522. * (using stop_machine()).
  2523. */
  2524. engine->submit_request = nop_submit_request;
  2525. /* Mark all executing requests as skipped */
  2526. spin_lock_irqsave(&engine->timeline->lock, flags);
  2527. list_for_each_entry(request, &engine->timeline->requests, link)
  2528. dma_fence_set_error(&request->fence, -EIO);
  2529. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  2530. /* Mark all pending requests as complete so that any concurrent
  2531. * (lockless) lookup doesn't try and wait upon the request as we
  2532. * reset it.
  2533. */
  2534. intel_engine_init_global_seqno(engine,
  2535. intel_engine_last_submit(engine));
  2536. /*
  2537. * Clear the execlists queue up before freeing the requests, as those
  2538. * are the ones that keep the context and ringbuffer backing objects
  2539. * pinned in place.
  2540. */
  2541. if (i915.enable_execlists) {
  2542. unsigned long flags;
  2543. spin_lock_irqsave(&engine->timeline->lock, flags);
  2544. i915_gem_request_put(engine->execlist_port[0].request);
  2545. i915_gem_request_put(engine->execlist_port[1].request);
  2546. memset(engine->execlist_port, 0, sizeof(engine->execlist_port));
  2547. engine->execlist_queue = RB_ROOT;
  2548. engine->execlist_first = NULL;
  2549. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  2550. }
  2551. }
  2552. static int __i915_gem_set_wedged_BKL(void *data)
  2553. {
  2554. struct drm_i915_private *i915 = data;
  2555. struct intel_engine_cs *engine;
  2556. enum intel_engine_id id;
  2557. for_each_engine(engine, i915, id)
  2558. engine_set_wedged(engine);
  2559. return 0;
  2560. }
  2561. void i915_gem_set_wedged(struct drm_i915_private *dev_priv)
  2562. {
  2563. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  2564. set_bit(I915_WEDGED, &dev_priv->gpu_error.flags);
  2565. /* Retire completed requests first so the list of inflight/incomplete
  2566. * requests is accurate and we don't try and mark successful requests
  2567. * as in error during __i915_gem_set_wedged_BKL().
  2568. */
  2569. i915_gem_retire_requests(dev_priv);
  2570. stop_machine(__i915_gem_set_wedged_BKL, dev_priv, NULL);
  2571. i915_gem_context_lost(dev_priv);
  2572. mod_delayed_work(dev_priv->wq, &dev_priv->gt.idle_work, 0);
  2573. }
  2574. bool i915_gem_unset_wedged(struct drm_i915_private *i915)
  2575. {
  2576. struct i915_gem_timeline *tl;
  2577. int i;
  2578. lockdep_assert_held(&i915->drm.struct_mutex);
  2579. if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
  2580. return true;
  2581. /* Before unwedging, make sure that all pending operations
  2582. * are flushed and errored out - we may have requests waiting upon
  2583. * third party fences. We marked all inflight requests as EIO, and
  2584. * every execbuf since returned EIO, for consistency we want all
  2585. * the currently pending requests to also be marked as EIO, which
  2586. * is done inside our nop_submit_request - and so we must wait.
  2587. *
  2588. * No more can be submitted until we reset the wedged bit.
  2589. */
  2590. list_for_each_entry(tl, &i915->gt.timelines, link) {
  2591. for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
  2592. struct drm_i915_gem_request *rq;
  2593. rq = i915_gem_active_peek(&tl->engine[i].last_request,
  2594. &i915->drm.struct_mutex);
  2595. if (!rq)
  2596. continue;
  2597. /* We can't use our normal waiter as we want to
  2598. * avoid recursively trying to handle the current
  2599. * reset. The basic dma_fence_default_wait() installs
  2600. * a callback for dma_fence_signal(), which is
  2601. * triggered by our nop handler (indirectly, the
  2602. * callback enables the signaler thread which is
  2603. * woken by the nop_submit_request() advancing the seqno
  2604. * and when the seqno passes the fence, the signaler
  2605. * then signals the fence waking us up).
  2606. */
  2607. if (dma_fence_default_wait(&rq->fence, true,
  2608. MAX_SCHEDULE_TIMEOUT) < 0)
  2609. return false;
  2610. }
  2611. }
  2612. /* Undo nop_submit_request. We prevent all new i915 requests from
  2613. * being queued (by disallowing execbuf whilst wedged) so having
  2614. * waited for all active requests above, we know the system is idle
  2615. * and do not have to worry about a thread being inside
  2616. * engine->submit_request() as we swap over. So unlike installing
  2617. * the nop_submit_request on reset, we can do this from normal
  2618. * context and do not require stop_machine().
  2619. */
  2620. intel_engines_reset_default_submission(i915);
  2621. smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
  2622. clear_bit(I915_WEDGED, &i915->gpu_error.flags);
  2623. return true;
  2624. }
  2625. static void
  2626. i915_gem_retire_work_handler(struct work_struct *work)
  2627. {
  2628. struct drm_i915_private *dev_priv =
  2629. container_of(work, typeof(*dev_priv), gt.retire_work.work);
  2630. struct drm_device *dev = &dev_priv->drm;
  2631. /* Come back later if the device is busy... */
  2632. if (mutex_trylock(&dev->struct_mutex)) {
  2633. i915_gem_retire_requests(dev_priv);
  2634. mutex_unlock(&dev->struct_mutex);
  2635. }
  2636. /* Keep the retire handler running until we are finally idle.
  2637. * We do not need to do this test under locking as in the worst-case
  2638. * we queue the retire worker once too often.
  2639. */
  2640. if (READ_ONCE(dev_priv->gt.awake)) {
  2641. i915_queue_hangcheck(dev_priv);
  2642. queue_delayed_work(dev_priv->wq,
  2643. &dev_priv->gt.retire_work,
  2644. round_jiffies_up_relative(HZ));
  2645. }
  2646. }
  2647. static void
  2648. i915_gem_idle_work_handler(struct work_struct *work)
  2649. {
  2650. struct drm_i915_private *dev_priv =
  2651. container_of(work, typeof(*dev_priv), gt.idle_work.work);
  2652. struct drm_device *dev = &dev_priv->drm;
  2653. struct intel_engine_cs *engine;
  2654. enum intel_engine_id id;
  2655. bool rearm_hangcheck;
  2656. if (!READ_ONCE(dev_priv->gt.awake))
  2657. return;
  2658. /*
  2659. * Wait for last execlists context complete, but bail out in case a
  2660. * new request is submitted.
  2661. */
  2662. wait_for(intel_engines_are_idle(dev_priv), 10);
  2663. if (READ_ONCE(dev_priv->gt.active_requests))
  2664. return;
  2665. rearm_hangcheck =
  2666. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  2667. if (!mutex_trylock(&dev->struct_mutex)) {
  2668. /* Currently busy, come back later */
  2669. mod_delayed_work(dev_priv->wq,
  2670. &dev_priv->gt.idle_work,
  2671. msecs_to_jiffies(50));
  2672. goto out_rearm;
  2673. }
  2674. /*
  2675. * New request retired after this work handler started, extend active
  2676. * period until next instance of the work.
  2677. */
  2678. if (work_pending(work))
  2679. goto out_unlock;
  2680. if (dev_priv->gt.active_requests)
  2681. goto out_unlock;
  2682. if (wait_for(intel_engines_are_idle(dev_priv), 10))
  2683. DRM_ERROR("Timeout waiting for engines to idle\n");
  2684. for_each_engine(engine, dev_priv, id) {
  2685. intel_engine_disarm_breadcrumbs(engine);
  2686. i915_gem_batch_pool_fini(&engine->batch_pool);
  2687. }
  2688. i915_gem_timelines_mark_idle(dev_priv);
  2689. GEM_BUG_ON(!dev_priv->gt.awake);
  2690. dev_priv->gt.awake = false;
  2691. rearm_hangcheck = false;
  2692. if (INTEL_GEN(dev_priv) >= 6)
  2693. gen6_rps_idle(dev_priv);
  2694. intel_runtime_pm_put(dev_priv);
  2695. out_unlock:
  2696. mutex_unlock(&dev->struct_mutex);
  2697. out_rearm:
  2698. if (rearm_hangcheck) {
  2699. GEM_BUG_ON(!dev_priv->gt.awake);
  2700. i915_queue_hangcheck(dev_priv);
  2701. }
  2702. }
  2703. void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
  2704. {
  2705. struct drm_i915_gem_object *obj = to_intel_bo(gem);
  2706. struct drm_i915_file_private *fpriv = file->driver_priv;
  2707. struct i915_vma *vma, *vn;
  2708. mutex_lock(&obj->base.dev->struct_mutex);
  2709. list_for_each_entry_safe(vma, vn, &obj->vma_list, obj_link)
  2710. if (vma->vm->file == fpriv)
  2711. i915_vma_close(vma);
  2712. if (i915_gem_object_is_active(obj) &&
  2713. !i915_gem_object_has_active_reference(obj)) {
  2714. i915_gem_object_set_active_reference(obj);
  2715. i915_gem_object_get(obj);
  2716. }
  2717. mutex_unlock(&obj->base.dev->struct_mutex);
  2718. }
  2719. static unsigned long to_wait_timeout(s64 timeout_ns)
  2720. {
  2721. if (timeout_ns < 0)
  2722. return MAX_SCHEDULE_TIMEOUT;
  2723. if (timeout_ns == 0)
  2724. return 0;
  2725. return nsecs_to_jiffies_timeout(timeout_ns);
  2726. }
  2727. /**
  2728. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2729. * @dev: drm device pointer
  2730. * @data: ioctl data blob
  2731. * @file: drm file pointer
  2732. *
  2733. * Returns 0 if successful, else an error is returned with the remaining time in
  2734. * the timeout parameter.
  2735. * -ETIME: object is still busy after timeout
  2736. * -ERESTARTSYS: signal interrupted the wait
  2737. * -ENONENT: object doesn't exist
  2738. * Also possible, but rare:
  2739. * -EAGAIN: GPU wedged
  2740. * -ENOMEM: damn
  2741. * -ENODEV: Internal IRQ fail
  2742. * -E?: The add request failed
  2743. *
  2744. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2745. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2746. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2747. * without holding struct_mutex the object may become re-busied before this
  2748. * function completes. A similar but shorter * race condition exists in the busy
  2749. * ioctl
  2750. */
  2751. int
  2752. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2753. {
  2754. struct drm_i915_gem_wait *args = data;
  2755. struct drm_i915_gem_object *obj;
  2756. ktime_t start;
  2757. long ret;
  2758. if (args->flags != 0)
  2759. return -EINVAL;
  2760. obj = i915_gem_object_lookup(file, args->bo_handle);
  2761. if (!obj)
  2762. return -ENOENT;
  2763. start = ktime_get();
  2764. ret = i915_gem_object_wait(obj,
  2765. I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
  2766. to_wait_timeout(args->timeout_ns),
  2767. to_rps_client(file));
  2768. if (args->timeout_ns > 0) {
  2769. args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
  2770. if (args->timeout_ns < 0)
  2771. args->timeout_ns = 0;
  2772. /*
  2773. * Apparently ktime isn't accurate enough and occasionally has a
  2774. * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
  2775. * things up to make the test happy. We allow up to 1 jiffy.
  2776. *
  2777. * This is a regression from the timespec->ktime conversion.
  2778. */
  2779. if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
  2780. args->timeout_ns = 0;
  2781. }
  2782. i915_gem_object_put(obj);
  2783. return ret;
  2784. }
  2785. static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
  2786. {
  2787. int ret, i;
  2788. for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
  2789. ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
  2790. if (ret)
  2791. return ret;
  2792. }
  2793. return 0;
  2794. }
  2795. static int wait_for_engine(struct intel_engine_cs *engine, int timeout_ms)
  2796. {
  2797. return wait_for(intel_engine_is_idle(engine), timeout_ms);
  2798. }
  2799. static int wait_for_engines(struct drm_i915_private *i915)
  2800. {
  2801. struct intel_engine_cs *engine;
  2802. enum intel_engine_id id;
  2803. for_each_engine(engine, i915, id) {
  2804. if (GEM_WARN_ON(wait_for_engine(engine, 50))) {
  2805. i915_gem_set_wedged(i915);
  2806. return -EIO;
  2807. }
  2808. GEM_BUG_ON(intel_engine_get_seqno(engine) !=
  2809. intel_engine_last_submit(engine));
  2810. }
  2811. return 0;
  2812. }
  2813. int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
  2814. {
  2815. int ret;
  2816. if (flags & I915_WAIT_LOCKED) {
  2817. struct i915_gem_timeline *tl;
  2818. lockdep_assert_held(&i915->drm.struct_mutex);
  2819. list_for_each_entry(tl, &i915->gt.timelines, link) {
  2820. ret = wait_for_timeline(tl, flags);
  2821. if (ret)
  2822. return ret;
  2823. }
  2824. i915_gem_retire_requests(i915);
  2825. GEM_BUG_ON(i915->gt.active_requests);
  2826. ret = wait_for_engines(i915);
  2827. } else {
  2828. ret = wait_for_timeline(&i915->gt.global_timeline, flags);
  2829. }
  2830. return ret;
  2831. }
  2832. static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
  2833. {
  2834. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU && !obj->cache_dirty)
  2835. return;
  2836. i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
  2837. obj->base.write_domain = 0;
  2838. }
  2839. void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
  2840. {
  2841. if (!READ_ONCE(obj->pin_display))
  2842. return;
  2843. mutex_lock(&obj->base.dev->struct_mutex);
  2844. __i915_gem_object_flush_for_display(obj);
  2845. mutex_unlock(&obj->base.dev->struct_mutex);
  2846. }
  2847. /**
  2848. * Moves a single object to the WC read, and possibly write domain.
  2849. * @obj: object to act on
  2850. * @write: ask for write access or read only
  2851. *
  2852. * This function returns when the move is complete, including waiting on
  2853. * flushes to occur.
  2854. */
  2855. int
  2856. i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
  2857. {
  2858. int ret;
  2859. lockdep_assert_held(&obj->base.dev->struct_mutex);
  2860. ret = i915_gem_object_wait(obj,
  2861. I915_WAIT_INTERRUPTIBLE |
  2862. I915_WAIT_LOCKED |
  2863. (write ? I915_WAIT_ALL : 0),
  2864. MAX_SCHEDULE_TIMEOUT,
  2865. NULL);
  2866. if (ret)
  2867. return ret;
  2868. if (obj->base.write_domain == I915_GEM_DOMAIN_WC)
  2869. return 0;
  2870. /* Flush and acquire obj->pages so that we are coherent through
  2871. * direct access in memory with previous cached writes through
  2872. * shmemfs and that our cache domain tracking remains valid.
  2873. * For example, if the obj->filp was moved to swap without us
  2874. * being notified and releasing the pages, we would mistakenly
  2875. * continue to assume that the obj remained out of the CPU cached
  2876. * domain.
  2877. */
  2878. ret = i915_gem_object_pin_pages(obj);
  2879. if (ret)
  2880. return ret;
  2881. flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
  2882. /* Serialise direct access to this object with the barriers for
  2883. * coherent writes from the GPU, by effectively invalidating the
  2884. * WC domain upon first access.
  2885. */
  2886. if ((obj->base.read_domains & I915_GEM_DOMAIN_WC) == 0)
  2887. mb();
  2888. /* It should now be out of any other write domains, and we can update
  2889. * the domain values for our changes.
  2890. */
  2891. GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_WC) != 0);
  2892. obj->base.read_domains |= I915_GEM_DOMAIN_WC;
  2893. if (write) {
  2894. obj->base.read_domains = I915_GEM_DOMAIN_WC;
  2895. obj->base.write_domain = I915_GEM_DOMAIN_WC;
  2896. obj->mm.dirty = true;
  2897. }
  2898. i915_gem_object_unpin_pages(obj);
  2899. return 0;
  2900. }
  2901. /**
  2902. * Moves a single object to the GTT read, and possibly write domain.
  2903. * @obj: object to act on
  2904. * @write: ask for write access or read only
  2905. *
  2906. * This function returns when the move is complete, including waiting on
  2907. * flushes to occur.
  2908. */
  2909. int
  2910. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  2911. {
  2912. int ret;
  2913. lockdep_assert_held(&obj->base.dev->struct_mutex);
  2914. ret = i915_gem_object_wait(obj,
  2915. I915_WAIT_INTERRUPTIBLE |
  2916. I915_WAIT_LOCKED |
  2917. (write ? I915_WAIT_ALL : 0),
  2918. MAX_SCHEDULE_TIMEOUT,
  2919. NULL);
  2920. if (ret)
  2921. return ret;
  2922. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  2923. return 0;
  2924. /* Flush and acquire obj->pages so that we are coherent through
  2925. * direct access in memory with previous cached writes through
  2926. * shmemfs and that our cache domain tracking remains valid.
  2927. * For example, if the obj->filp was moved to swap without us
  2928. * being notified and releasing the pages, we would mistakenly
  2929. * continue to assume that the obj remained out of the CPU cached
  2930. * domain.
  2931. */
  2932. ret = i915_gem_object_pin_pages(obj);
  2933. if (ret)
  2934. return ret;
  2935. flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
  2936. /* Serialise direct access to this object with the barriers for
  2937. * coherent writes from the GPU, by effectively invalidating the
  2938. * GTT domain upon first access.
  2939. */
  2940. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2941. mb();
  2942. /* It should now be out of any other write domains, and we can update
  2943. * the domain values for our changes.
  2944. */
  2945. GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2946. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2947. if (write) {
  2948. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  2949. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  2950. obj->mm.dirty = true;
  2951. }
  2952. i915_gem_object_unpin_pages(obj);
  2953. return 0;
  2954. }
  2955. /**
  2956. * Changes the cache-level of an object across all VMA.
  2957. * @obj: object to act on
  2958. * @cache_level: new cache level to set for the object
  2959. *
  2960. * After this function returns, the object will be in the new cache-level
  2961. * across all GTT and the contents of the backing storage will be coherent,
  2962. * with respect to the new cache-level. In order to keep the backing storage
  2963. * coherent for all users, we only allow a single cache level to be set
  2964. * globally on the object and prevent it from being changed whilst the
  2965. * hardware is reading from the object. That is if the object is currently
  2966. * on the scanout it will be set to uncached (or equivalent display
  2967. * cache coherency) and all non-MOCS GPU access will also be uncached so
  2968. * that all direct access to the scanout remains coherent.
  2969. */
  2970. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  2971. enum i915_cache_level cache_level)
  2972. {
  2973. struct i915_vma *vma;
  2974. int ret;
  2975. lockdep_assert_held(&obj->base.dev->struct_mutex);
  2976. if (obj->cache_level == cache_level)
  2977. return 0;
  2978. /* Inspect the list of currently bound VMA and unbind any that would
  2979. * be invalid given the new cache-level. This is principally to
  2980. * catch the issue of the CS prefetch crossing page boundaries and
  2981. * reading an invalid PTE on older architectures.
  2982. */
  2983. restart:
  2984. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  2985. if (!drm_mm_node_allocated(&vma->node))
  2986. continue;
  2987. if (i915_vma_is_pinned(vma)) {
  2988. DRM_DEBUG("can not change the cache level of pinned objects\n");
  2989. return -EBUSY;
  2990. }
  2991. if (i915_gem_valid_gtt_space(vma, cache_level))
  2992. continue;
  2993. ret = i915_vma_unbind(vma);
  2994. if (ret)
  2995. return ret;
  2996. /* As unbinding may affect other elements in the
  2997. * obj->vma_list (due to side-effects from retiring
  2998. * an active vma), play safe and restart the iterator.
  2999. */
  3000. goto restart;
  3001. }
  3002. /* We can reuse the existing drm_mm nodes but need to change the
  3003. * cache-level on the PTE. We could simply unbind them all and
  3004. * rebind with the correct cache-level on next use. However since
  3005. * we already have a valid slot, dma mapping, pages etc, we may as
  3006. * rewrite the PTE in the belief that doing so tramples upon less
  3007. * state and so involves less work.
  3008. */
  3009. if (obj->bind_count) {
  3010. /* Before we change the PTE, the GPU must not be accessing it.
  3011. * If we wait upon the object, we know that all the bound
  3012. * VMA are no longer active.
  3013. */
  3014. ret = i915_gem_object_wait(obj,
  3015. I915_WAIT_INTERRUPTIBLE |
  3016. I915_WAIT_LOCKED |
  3017. I915_WAIT_ALL,
  3018. MAX_SCHEDULE_TIMEOUT,
  3019. NULL);
  3020. if (ret)
  3021. return ret;
  3022. if (!HAS_LLC(to_i915(obj->base.dev)) &&
  3023. cache_level != I915_CACHE_NONE) {
  3024. /* Access to snoopable pages through the GTT is
  3025. * incoherent and on some machines causes a hard
  3026. * lockup. Relinquish the CPU mmaping to force
  3027. * userspace to refault in the pages and we can
  3028. * then double check if the GTT mapping is still
  3029. * valid for that pointer access.
  3030. */
  3031. i915_gem_release_mmap(obj);
  3032. /* As we no longer need a fence for GTT access,
  3033. * we can relinquish it now (and so prevent having
  3034. * to steal a fence from someone else on the next
  3035. * fence request). Note GPU activity would have
  3036. * dropped the fence as all snoopable access is
  3037. * supposed to be linear.
  3038. */
  3039. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  3040. ret = i915_vma_put_fence(vma);
  3041. if (ret)
  3042. return ret;
  3043. }
  3044. } else {
  3045. /* We either have incoherent backing store and
  3046. * so no GTT access or the architecture is fully
  3047. * coherent. In such cases, existing GTT mmaps
  3048. * ignore the cache bit in the PTE and we can
  3049. * rewrite it without confusing the GPU or having
  3050. * to force userspace to fault back in its mmaps.
  3051. */
  3052. }
  3053. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  3054. if (!drm_mm_node_allocated(&vma->node))
  3055. continue;
  3056. ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
  3057. if (ret)
  3058. return ret;
  3059. }
  3060. }
  3061. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU &&
  3062. i915_gem_object_is_coherent(obj))
  3063. obj->cache_dirty = true;
  3064. list_for_each_entry(vma, &obj->vma_list, obj_link)
  3065. vma->node.color = cache_level;
  3066. obj->cache_level = cache_level;
  3067. return 0;
  3068. }
  3069. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  3070. struct drm_file *file)
  3071. {
  3072. struct drm_i915_gem_caching *args = data;
  3073. struct drm_i915_gem_object *obj;
  3074. int err = 0;
  3075. rcu_read_lock();
  3076. obj = i915_gem_object_lookup_rcu(file, args->handle);
  3077. if (!obj) {
  3078. err = -ENOENT;
  3079. goto out;
  3080. }
  3081. switch (obj->cache_level) {
  3082. case I915_CACHE_LLC:
  3083. case I915_CACHE_L3_LLC:
  3084. args->caching = I915_CACHING_CACHED;
  3085. break;
  3086. case I915_CACHE_WT:
  3087. args->caching = I915_CACHING_DISPLAY;
  3088. break;
  3089. default:
  3090. args->caching = I915_CACHING_NONE;
  3091. break;
  3092. }
  3093. out:
  3094. rcu_read_unlock();
  3095. return err;
  3096. }
  3097. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  3098. struct drm_file *file)
  3099. {
  3100. struct drm_i915_private *i915 = to_i915(dev);
  3101. struct drm_i915_gem_caching *args = data;
  3102. struct drm_i915_gem_object *obj;
  3103. enum i915_cache_level level;
  3104. int ret = 0;
  3105. switch (args->caching) {
  3106. case I915_CACHING_NONE:
  3107. level = I915_CACHE_NONE;
  3108. break;
  3109. case I915_CACHING_CACHED:
  3110. /*
  3111. * Due to a HW issue on BXT A stepping, GPU stores via a
  3112. * snooped mapping may leave stale data in a corresponding CPU
  3113. * cacheline, whereas normally such cachelines would get
  3114. * invalidated.
  3115. */
  3116. if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
  3117. return -ENODEV;
  3118. level = I915_CACHE_LLC;
  3119. break;
  3120. case I915_CACHING_DISPLAY:
  3121. level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
  3122. break;
  3123. default:
  3124. return -EINVAL;
  3125. }
  3126. obj = i915_gem_object_lookup(file, args->handle);
  3127. if (!obj)
  3128. return -ENOENT;
  3129. if (obj->cache_level == level)
  3130. goto out;
  3131. ret = i915_gem_object_wait(obj,
  3132. I915_WAIT_INTERRUPTIBLE,
  3133. MAX_SCHEDULE_TIMEOUT,
  3134. to_rps_client(file));
  3135. if (ret)
  3136. goto out;
  3137. ret = i915_mutex_lock_interruptible(dev);
  3138. if (ret)
  3139. goto out;
  3140. ret = i915_gem_object_set_cache_level(obj, level);
  3141. mutex_unlock(&dev->struct_mutex);
  3142. out:
  3143. i915_gem_object_put(obj);
  3144. return ret;
  3145. }
  3146. /*
  3147. * Prepare buffer for display plane (scanout, cursors, etc).
  3148. * Can be called from an uninterruptible phase (modesetting) and allows
  3149. * any flushes to be pipelined (for pageflips).
  3150. */
  3151. struct i915_vma *
  3152. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  3153. u32 alignment,
  3154. const struct i915_ggtt_view *view)
  3155. {
  3156. struct i915_vma *vma;
  3157. int ret;
  3158. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3159. /* Mark the pin_display early so that we account for the
  3160. * display coherency whilst setting up the cache domains.
  3161. */
  3162. obj->pin_display++;
  3163. /* The display engine is not coherent with the LLC cache on gen6. As
  3164. * a result, we make sure that the pinning that is about to occur is
  3165. * done with uncached PTEs. This is lowest common denominator for all
  3166. * chipsets.
  3167. *
  3168. * However for gen6+, we could do better by using the GFDT bit instead
  3169. * of uncaching, which would allow us to flush all the LLC-cached data
  3170. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  3171. */
  3172. ret = i915_gem_object_set_cache_level(obj,
  3173. HAS_WT(to_i915(obj->base.dev)) ?
  3174. I915_CACHE_WT : I915_CACHE_NONE);
  3175. if (ret) {
  3176. vma = ERR_PTR(ret);
  3177. goto err_unpin_display;
  3178. }
  3179. /* As the user may map the buffer once pinned in the display plane
  3180. * (e.g. libkms for the bootup splash), we have to ensure that we
  3181. * always use map_and_fenceable for all scanout buffers. However,
  3182. * it may simply be too big to fit into mappable, in which case
  3183. * put it anyway and hope that userspace can cope (but always first
  3184. * try to preserve the existing ABI).
  3185. */
  3186. vma = ERR_PTR(-ENOSPC);
  3187. if (!view || view->type == I915_GGTT_VIEW_NORMAL)
  3188. vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
  3189. PIN_MAPPABLE | PIN_NONBLOCK);
  3190. if (IS_ERR(vma)) {
  3191. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  3192. unsigned int flags;
  3193. /* Valleyview is definitely limited to scanning out the first
  3194. * 512MiB. Lets presume this behaviour was inherited from the
  3195. * g4x display engine and that all earlier gen are similarly
  3196. * limited. Testing suggests that it is a little more
  3197. * complicated than this. For example, Cherryview appears quite
  3198. * happy to scanout from anywhere within its global aperture.
  3199. */
  3200. flags = 0;
  3201. if (HAS_GMCH_DISPLAY(i915))
  3202. flags = PIN_MAPPABLE;
  3203. vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
  3204. }
  3205. if (IS_ERR(vma))
  3206. goto err_unpin_display;
  3207. vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
  3208. /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
  3209. __i915_gem_object_flush_for_display(obj);
  3210. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  3211. /* It should now be out of any other write domains, and we can update
  3212. * the domain values for our changes.
  3213. */
  3214. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3215. return vma;
  3216. err_unpin_display:
  3217. obj->pin_display--;
  3218. return vma;
  3219. }
  3220. void
  3221. i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
  3222. {
  3223. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  3224. if (WARN_ON(vma->obj->pin_display == 0))
  3225. return;
  3226. if (--vma->obj->pin_display == 0)
  3227. vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
  3228. /* Bump the LRU to try and avoid premature eviction whilst flipping */
  3229. i915_gem_object_bump_inactive_ggtt(vma->obj);
  3230. i915_vma_unpin(vma);
  3231. }
  3232. /**
  3233. * Moves a single object to the CPU read, and possibly write domain.
  3234. * @obj: object to act on
  3235. * @write: requesting write or read-only access
  3236. *
  3237. * This function returns when the move is complete, including waiting on
  3238. * flushes to occur.
  3239. */
  3240. int
  3241. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  3242. {
  3243. int ret;
  3244. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3245. ret = i915_gem_object_wait(obj,
  3246. I915_WAIT_INTERRUPTIBLE |
  3247. I915_WAIT_LOCKED |
  3248. (write ? I915_WAIT_ALL : 0),
  3249. MAX_SCHEDULE_TIMEOUT,
  3250. NULL);
  3251. if (ret)
  3252. return ret;
  3253. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  3254. return 0;
  3255. flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
  3256. /* Flush the CPU cache if it's still invalid. */
  3257. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  3258. i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
  3259. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  3260. }
  3261. /* It should now be out of any other write domains, and we can update
  3262. * the domain values for our changes.
  3263. */
  3264. GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  3265. /* If we're writing through the CPU, then the GPU read domains will
  3266. * need to be invalidated at next use.
  3267. */
  3268. if (write) {
  3269. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3270. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3271. }
  3272. return 0;
  3273. }
  3274. /* Throttle our rendering by waiting until the ring has completed our requests
  3275. * emitted over 20 msec ago.
  3276. *
  3277. * Note that if we were to use the current jiffies each time around the loop,
  3278. * we wouldn't escape the function with any frames outstanding if the time to
  3279. * render a frame was over 20ms.
  3280. *
  3281. * This should get us reasonable parallelism between CPU and GPU but also
  3282. * relatively low latency when blocking on a particular request to finish.
  3283. */
  3284. static int
  3285. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  3286. {
  3287. struct drm_i915_private *dev_priv = to_i915(dev);
  3288. struct drm_i915_file_private *file_priv = file->driver_priv;
  3289. unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
  3290. struct drm_i915_gem_request *request, *target = NULL;
  3291. long ret;
  3292. /* ABI: return -EIO if already wedged */
  3293. if (i915_terminally_wedged(&dev_priv->gpu_error))
  3294. return -EIO;
  3295. spin_lock(&file_priv->mm.lock);
  3296. list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
  3297. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3298. break;
  3299. if (target) {
  3300. list_del(&target->client_link);
  3301. target->file_priv = NULL;
  3302. }
  3303. target = request;
  3304. }
  3305. if (target)
  3306. i915_gem_request_get(target);
  3307. spin_unlock(&file_priv->mm.lock);
  3308. if (target == NULL)
  3309. return 0;
  3310. ret = i915_wait_request(target,
  3311. I915_WAIT_INTERRUPTIBLE,
  3312. MAX_SCHEDULE_TIMEOUT);
  3313. i915_gem_request_put(target);
  3314. return ret < 0 ? ret : 0;
  3315. }
  3316. struct i915_vma *
  3317. i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
  3318. const struct i915_ggtt_view *view,
  3319. u64 size,
  3320. u64 alignment,
  3321. u64 flags)
  3322. {
  3323. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  3324. struct i915_address_space *vm = &dev_priv->ggtt.base;
  3325. struct i915_vma *vma;
  3326. int ret;
  3327. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3328. vma = i915_vma_instance(obj, vm, view);
  3329. if (unlikely(IS_ERR(vma)))
  3330. return vma;
  3331. if (i915_vma_misplaced(vma, size, alignment, flags)) {
  3332. if (flags & PIN_NONBLOCK &&
  3333. (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)))
  3334. return ERR_PTR(-ENOSPC);
  3335. if (flags & PIN_MAPPABLE) {
  3336. /* If the required space is larger than the available
  3337. * aperture, we will not able to find a slot for the
  3338. * object and unbinding the object now will be in
  3339. * vain. Worse, doing so may cause us to ping-pong
  3340. * the object in and out of the Global GTT and
  3341. * waste a lot of cycles under the mutex.
  3342. */
  3343. if (vma->fence_size > dev_priv->ggtt.mappable_end)
  3344. return ERR_PTR(-E2BIG);
  3345. /* If NONBLOCK is set the caller is optimistically
  3346. * trying to cache the full object within the mappable
  3347. * aperture, and *must* have a fallback in place for
  3348. * situations where we cannot bind the object. We
  3349. * can be a little more lax here and use the fallback
  3350. * more often to avoid costly migrations of ourselves
  3351. * and other objects within the aperture.
  3352. *
  3353. * Half-the-aperture is used as a simple heuristic.
  3354. * More interesting would to do search for a free
  3355. * block prior to making the commitment to unbind.
  3356. * That caters for the self-harm case, and with a
  3357. * little more heuristics (e.g. NOFAULT, NOEVICT)
  3358. * we could try to minimise harm to others.
  3359. */
  3360. if (flags & PIN_NONBLOCK &&
  3361. vma->fence_size > dev_priv->ggtt.mappable_end / 2)
  3362. return ERR_PTR(-ENOSPC);
  3363. }
  3364. WARN(i915_vma_is_pinned(vma),
  3365. "bo is already pinned in ggtt with incorrect alignment:"
  3366. " offset=%08x, req.alignment=%llx,"
  3367. " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
  3368. i915_ggtt_offset(vma), alignment,
  3369. !!(flags & PIN_MAPPABLE),
  3370. i915_vma_is_map_and_fenceable(vma));
  3371. ret = i915_vma_unbind(vma);
  3372. if (ret)
  3373. return ERR_PTR(ret);
  3374. }
  3375. ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
  3376. if (ret)
  3377. return ERR_PTR(ret);
  3378. return vma;
  3379. }
  3380. static __always_inline unsigned int __busy_read_flag(unsigned int id)
  3381. {
  3382. /* Note that we could alias engines in the execbuf API, but
  3383. * that would be very unwise as it prevents userspace from
  3384. * fine control over engine selection. Ahem.
  3385. *
  3386. * This should be something like EXEC_MAX_ENGINE instead of
  3387. * I915_NUM_ENGINES.
  3388. */
  3389. BUILD_BUG_ON(I915_NUM_ENGINES > 16);
  3390. return 0x10000 << id;
  3391. }
  3392. static __always_inline unsigned int __busy_write_id(unsigned int id)
  3393. {
  3394. /* The uABI guarantees an active writer is also amongst the read
  3395. * engines. This would be true if we accessed the activity tracking
  3396. * under the lock, but as we perform the lookup of the object and
  3397. * its activity locklessly we can not guarantee that the last_write
  3398. * being active implies that we have set the same engine flag from
  3399. * last_read - hence we always set both read and write busy for
  3400. * last_write.
  3401. */
  3402. return id | __busy_read_flag(id);
  3403. }
  3404. static __always_inline unsigned int
  3405. __busy_set_if_active(const struct dma_fence *fence,
  3406. unsigned int (*flag)(unsigned int id))
  3407. {
  3408. struct drm_i915_gem_request *rq;
  3409. /* We have to check the current hw status of the fence as the uABI
  3410. * guarantees forward progress. We could rely on the idle worker
  3411. * to eventually flush us, but to minimise latency just ask the
  3412. * hardware.
  3413. *
  3414. * Note we only report on the status of native fences.
  3415. */
  3416. if (!dma_fence_is_i915(fence))
  3417. return 0;
  3418. /* opencode to_request() in order to avoid const warnings */
  3419. rq = container_of(fence, struct drm_i915_gem_request, fence);
  3420. if (i915_gem_request_completed(rq))
  3421. return 0;
  3422. return flag(rq->engine->uabi_id);
  3423. }
  3424. static __always_inline unsigned int
  3425. busy_check_reader(const struct dma_fence *fence)
  3426. {
  3427. return __busy_set_if_active(fence, __busy_read_flag);
  3428. }
  3429. static __always_inline unsigned int
  3430. busy_check_writer(const struct dma_fence *fence)
  3431. {
  3432. if (!fence)
  3433. return 0;
  3434. return __busy_set_if_active(fence, __busy_write_id);
  3435. }
  3436. int
  3437. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3438. struct drm_file *file)
  3439. {
  3440. struct drm_i915_gem_busy *args = data;
  3441. struct drm_i915_gem_object *obj;
  3442. struct reservation_object_list *list;
  3443. unsigned int seq;
  3444. int err;
  3445. err = -ENOENT;
  3446. rcu_read_lock();
  3447. obj = i915_gem_object_lookup_rcu(file, args->handle);
  3448. if (!obj)
  3449. goto out;
  3450. /* A discrepancy here is that we do not report the status of
  3451. * non-i915 fences, i.e. even though we may report the object as idle,
  3452. * a call to set-domain may still stall waiting for foreign rendering.
  3453. * This also means that wait-ioctl may report an object as busy,
  3454. * where busy-ioctl considers it idle.
  3455. *
  3456. * We trade the ability to warn of foreign fences to report on which
  3457. * i915 engines are active for the object.
  3458. *
  3459. * Alternatively, we can trade that extra information on read/write
  3460. * activity with
  3461. * args->busy =
  3462. * !reservation_object_test_signaled_rcu(obj->resv, true);
  3463. * to report the overall busyness. This is what the wait-ioctl does.
  3464. *
  3465. */
  3466. retry:
  3467. seq = raw_read_seqcount(&obj->resv->seq);
  3468. /* Translate the exclusive fence to the READ *and* WRITE engine */
  3469. args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
  3470. /* Translate shared fences to READ set of engines */
  3471. list = rcu_dereference(obj->resv->fence);
  3472. if (list) {
  3473. unsigned int shared_count = list->shared_count, i;
  3474. for (i = 0; i < shared_count; ++i) {
  3475. struct dma_fence *fence =
  3476. rcu_dereference(list->shared[i]);
  3477. args->busy |= busy_check_reader(fence);
  3478. }
  3479. }
  3480. if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
  3481. goto retry;
  3482. err = 0;
  3483. out:
  3484. rcu_read_unlock();
  3485. return err;
  3486. }
  3487. int
  3488. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3489. struct drm_file *file_priv)
  3490. {
  3491. return i915_gem_ring_throttle(dev, file_priv);
  3492. }
  3493. int
  3494. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3495. struct drm_file *file_priv)
  3496. {
  3497. struct drm_i915_private *dev_priv = to_i915(dev);
  3498. struct drm_i915_gem_madvise *args = data;
  3499. struct drm_i915_gem_object *obj;
  3500. int err;
  3501. switch (args->madv) {
  3502. case I915_MADV_DONTNEED:
  3503. case I915_MADV_WILLNEED:
  3504. break;
  3505. default:
  3506. return -EINVAL;
  3507. }
  3508. obj = i915_gem_object_lookup(file_priv, args->handle);
  3509. if (!obj)
  3510. return -ENOENT;
  3511. err = mutex_lock_interruptible(&obj->mm.lock);
  3512. if (err)
  3513. goto out;
  3514. if (obj->mm.pages &&
  3515. i915_gem_object_is_tiled(obj) &&
  3516. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  3517. if (obj->mm.madv == I915_MADV_WILLNEED) {
  3518. GEM_BUG_ON(!obj->mm.quirked);
  3519. __i915_gem_object_unpin_pages(obj);
  3520. obj->mm.quirked = false;
  3521. }
  3522. if (args->madv == I915_MADV_WILLNEED) {
  3523. GEM_BUG_ON(obj->mm.quirked);
  3524. __i915_gem_object_pin_pages(obj);
  3525. obj->mm.quirked = true;
  3526. }
  3527. }
  3528. if (obj->mm.madv != __I915_MADV_PURGED)
  3529. obj->mm.madv = args->madv;
  3530. /* if the object is no longer attached, discard its backing storage */
  3531. if (obj->mm.madv == I915_MADV_DONTNEED && !obj->mm.pages)
  3532. i915_gem_object_truncate(obj);
  3533. args->retained = obj->mm.madv != __I915_MADV_PURGED;
  3534. mutex_unlock(&obj->mm.lock);
  3535. out:
  3536. i915_gem_object_put(obj);
  3537. return err;
  3538. }
  3539. static void
  3540. frontbuffer_retire(struct i915_gem_active *active,
  3541. struct drm_i915_gem_request *request)
  3542. {
  3543. struct drm_i915_gem_object *obj =
  3544. container_of(active, typeof(*obj), frontbuffer_write);
  3545. intel_fb_obj_flush(obj, ORIGIN_CS);
  3546. }
  3547. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3548. const struct drm_i915_gem_object_ops *ops)
  3549. {
  3550. mutex_init(&obj->mm.lock);
  3551. INIT_LIST_HEAD(&obj->global_link);
  3552. INIT_LIST_HEAD(&obj->userfault_link);
  3553. INIT_LIST_HEAD(&obj->obj_exec_link);
  3554. INIT_LIST_HEAD(&obj->vma_list);
  3555. INIT_LIST_HEAD(&obj->batch_pool_link);
  3556. obj->ops = ops;
  3557. reservation_object_init(&obj->__builtin_resv);
  3558. obj->resv = &obj->__builtin_resv;
  3559. obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
  3560. init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
  3561. obj->mm.madv = I915_MADV_WILLNEED;
  3562. INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
  3563. mutex_init(&obj->mm.get_page.lock);
  3564. i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
  3565. }
  3566. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3567. .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
  3568. I915_GEM_OBJECT_IS_SHRINKABLE,
  3569. .get_pages = i915_gem_object_get_pages_gtt,
  3570. .put_pages = i915_gem_object_put_pages_gtt,
  3571. .pwrite = i915_gem_object_pwrite_gtt,
  3572. };
  3573. struct drm_i915_gem_object *
  3574. i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
  3575. {
  3576. struct drm_i915_gem_object *obj;
  3577. struct address_space *mapping;
  3578. gfp_t mask;
  3579. int ret;
  3580. /* There is a prevalence of the assumption that we fit the object's
  3581. * page count inside a 32bit _signed_ variable. Let's document this and
  3582. * catch if we ever need to fix it. In the meantime, if you do spot
  3583. * such a local variable, please consider fixing!
  3584. */
  3585. if (size >> PAGE_SHIFT > INT_MAX)
  3586. return ERR_PTR(-E2BIG);
  3587. if (overflows_type(size, obj->base.size))
  3588. return ERR_PTR(-E2BIG);
  3589. obj = i915_gem_object_alloc(dev_priv);
  3590. if (obj == NULL)
  3591. return ERR_PTR(-ENOMEM);
  3592. ret = drm_gem_object_init(&dev_priv->drm, &obj->base, size);
  3593. if (ret)
  3594. goto fail;
  3595. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3596. if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
  3597. /* 965gm cannot relocate objects above 4GiB. */
  3598. mask &= ~__GFP_HIGHMEM;
  3599. mask |= __GFP_DMA32;
  3600. }
  3601. mapping = obj->base.filp->f_mapping;
  3602. mapping_set_gfp_mask(mapping, mask);
  3603. i915_gem_object_init(obj, &i915_gem_object_ops);
  3604. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3605. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3606. if (HAS_LLC(dev_priv)) {
  3607. /* On some devices, we can have the GPU use the LLC (the CPU
  3608. * cache) for about a 10% performance improvement
  3609. * compared to uncached. Graphics requests other than
  3610. * display scanout are coherent with the CPU in
  3611. * accessing this cache. This means in this mode we
  3612. * don't need to clflush on the CPU side, and on the
  3613. * GPU side we only need to flush internal caches to
  3614. * get data visible to the CPU.
  3615. *
  3616. * However, we maintain the display planes as UC, and so
  3617. * need to rebind when first used as such.
  3618. */
  3619. obj->cache_level = I915_CACHE_LLC;
  3620. } else
  3621. obj->cache_level = I915_CACHE_NONE;
  3622. trace_i915_gem_object_create(obj);
  3623. return obj;
  3624. fail:
  3625. i915_gem_object_free(obj);
  3626. return ERR_PTR(ret);
  3627. }
  3628. static bool discard_backing_storage(struct drm_i915_gem_object *obj)
  3629. {
  3630. /* If we are the last user of the backing storage (be it shmemfs
  3631. * pages or stolen etc), we know that the pages are going to be
  3632. * immediately released. In this case, we can then skip copying
  3633. * back the contents from the GPU.
  3634. */
  3635. if (obj->mm.madv != I915_MADV_WILLNEED)
  3636. return false;
  3637. if (obj->base.filp == NULL)
  3638. return true;
  3639. /* At first glance, this looks racy, but then again so would be
  3640. * userspace racing mmap against close. However, the first external
  3641. * reference to the filp can only be obtained through the
  3642. * i915_gem_mmap_ioctl() which safeguards us against the user
  3643. * acquiring such a reference whilst we are in the middle of
  3644. * freeing the object.
  3645. */
  3646. return atomic_long_read(&obj->base.filp->f_count) == 1;
  3647. }
  3648. static void __i915_gem_free_objects(struct drm_i915_private *i915,
  3649. struct llist_node *freed)
  3650. {
  3651. struct drm_i915_gem_object *obj, *on;
  3652. mutex_lock(&i915->drm.struct_mutex);
  3653. intel_runtime_pm_get(i915);
  3654. llist_for_each_entry(obj, freed, freed) {
  3655. struct i915_vma *vma, *vn;
  3656. trace_i915_gem_object_destroy(obj);
  3657. GEM_BUG_ON(i915_gem_object_is_active(obj));
  3658. list_for_each_entry_safe(vma, vn,
  3659. &obj->vma_list, obj_link) {
  3660. GEM_BUG_ON(!i915_vma_is_ggtt(vma));
  3661. GEM_BUG_ON(i915_vma_is_active(vma));
  3662. vma->flags &= ~I915_VMA_PIN_MASK;
  3663. i915_vma_close(vma);
  3664. }
  3665. GEM_BUG_ON(!list_empty(&obj->vma_list));
  3666. GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
  3667. list_del(&obj->global_link);
  3668. }
  3669. intel_runtime_pm_put(i915);
  3670. mutex_unlock(&i915->drm.struct_mutex);
  3671. cond_resched();
  3672. llist_for_each_entry_safe(obj, on, freed, freed) {
  3673. GEM_BUG_ON(obj->bind_count);
  3674. GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
  3675. if (obj->ops->release)
  3676. obj->ops->release(obj);
  3677. if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
  3678. atomic_set(&obj->mm.pages_pin_count, 0);
  3679. __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
  3680. GEM_BUG_ON(obj->mm.pages);
  3681. if (obj->base.import_attach)
  3682. drm_prime_gem_destroy(&obj->base, NULL);
  3683. reservation_object_fini(&obj->__builtin_resv);
  3684. drm_gem_object_release(&obj->base);
  3685. i915_gem_info_remove_obj(i915, obj->base.size);
  3686. kfree(obj->bit_17);
  3687. i915_gem_object_free(obj);
  3688. }
  3689. }
  3690. static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
  3691. {
  3692. struct llist_node *freed;
  3693. freed = llist_del_all(&i915->mm.free_list);
  3694. if (unlikely(freed))
  3695. __i915_gem_free_objects(i915, freed);
  3696. }
  3697. static void __i915_gem_free_work(struct work_struct *work)
  3698. {
  3699. struct drm_i915_private *i915 =
  3700. container_of(work, struct drm_i915_private, mm.free_work);
  3701. struct llist_node *freed;
  3702. /* All file-owned VMA should have been released by this point through
  3703. * i915_gem_close_object(), or earlier by i915_gem_context_close().
  3704. * However, the object may also be bound into the global GTT (e.g.
  3705. * older GPUs without per-process support, or for direct access through
  3706. * the GTT either for the user or for scanout). Those VMA still need to
  3707. * unbound now.
  3708. */
  3709. while ((freed = llist_del_all(&i915->mm.free_list))) {
  3710. __i915_gem_free_objects(i915, freed);
  3711. if (need_resched())
  3712. break;
  3713. }
  3714. }
  3715. static void __i915_gem_free_object_rcu(struct rcu_head *head)
  3716. {
  3717. struct drm_i915_gem_object *obj =
  3718. container_of(head, typeof(*obj), rcu);
  3719. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  3720. /* We can't simply use call_rcu() from i915_gem_free_object()
  3721. * as we need to block whilst unbinding, and the call_rcu
  3722. * task may be called from softirq context. So we take a
  3723. * detour through a worker.
  3724. */
  3725. if (llist_add(&obj->freed, &i915->mm.free_list))
  3726. schedule_work(&i915->mm.free_work);
  3727. }
  3728. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3729. {
  3730. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3731. if (obj->mm.quirked)
  3732. __i915_gem_object_unpin_pages(obj);
  3733. if (discard_backing_storage(obj))
  3734. obj->mm.madv = I915_MADV_DONTNEED;
  3735. /* Before we free the object, make sure any pure RCU-only
  3736. * read-side critical sections are complete, e.g.
  3737. * i915_gem_busy_ioctl(). For the corresponding synchronized
  3738. * lookup see i915_gem_object_lookup_rcu().
  3739. */
  3740. call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
  3741. }
  3742. void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
  3743. {
  3744. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3745. GEM_BUG_ON(i915_gem_object_has_active_reference(obj));
  3746. if (i915_gem_object_is_active(obj))
  3747. i915_gem_object_set_active_reference(obj);
  3748. else
  3749. i915_gem_object_put(obj);
  3750. }
  3751. static void assert_kernel_context_is_current(struct drm_i915_private *dev_priv)
  3752. {
  3753. struct intel_engine_cs *engine;
  3754. enum intel_engine_id id;
  3755. for_each_engine(engine, dev_priv, id)
  3756. GEM_BUG_ON(engine->last_retired_context &&
  3757. !i915_gem_context_is_kernel(engine->last_retired_context));
  3758. }
  3759. void i915_gem_sanitize(struct drm_i915_private *i915)
  3760. {
  3761. /*
  3762. * If we inherit context state from the BIOS or earlier occupants
  3763. * of the GPU, the GPU may be in an inconsistent state when we
  3764. * try to take over. The only way to remove the earlier state
  3765. * is by resetting. However, resetting on earlier gen is tricky as
  3766. * it may impact the display and we are uncertain about the stability
  3767. * of the reset, so this could be applied to even earlier gen.
  3768. */
  3769. if (INTEL_GEN(i915) >= 5) {
  3770. int reset = intel_gpu_reset(i915, ALL_ENGINES);
  3771. WARN_ON(reset && reset != -ENODEV);
  3772. }
  3773. }
  3774. int i915_gem_suspend(struct drm_i915_private *dev_priv)
  3775. {
  3776. struct drm_device *dev = &dev_priv->drm;
  3777. int ret;
  3778. intel_runtime_pm_get(dev_priv);
  3779. intel_suspend_gt_powersave(dev_priv);
  3780. mutex_lock(&dev->struct_mutex);
  3781. /* We have to flush all the executing contexts to main memory so
  3782. * that they can saved in the hibernation image. To ensure the last
  3783. * context image is coherent, we have to switch away from it. That
  3784. * leaves the dev_priv->kernel_context still active when
  3785. * we actually suspend, and its image in memory may not match the GPU
  3786. * state. Fortunately, the kernel_context is disposable and we do
  3787. * not rely on its state.
  3788. */
  3789. ret = i915_gem_switch_to_kernel_context(dev_priv);
  3790. if (ret)
  3791. goto err_unlock;
  3792. ret = i915_gem_wait_for_idle(dev_priv,
  3793. I915_WAIT_INTERRUPTIBLE |
  3794. I915_WAIT_LOCKED);
  3795. if (ret)
  3796. goto err_unlock;
  3797. assert_kernel_context_is_current(dev_priv);
  3798. i915_gem_context_lost(dev_priv);
  3799. mutex_unlock(&dev->struct_mutex);
  3800. intel_guc_suspend(dev_priv);
  3801. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  3802. cancel_delayed_work_sync(&dev_priv->gt.retire_work);
  3803. /* As the idle_work is rearming if it detects a race, play safe and
  3804. * repeat the flush until it is definitely idle.
  3805. */
  3806. while (flush_delayed_work(&dev_priv->gt.idle_work))
  3807. ;
  3808. i915_gem_drain_freed_objects(dev_priv);
  3809. /* Assert that we sucessfully flushed all the work and
  3810. * reset the GPU back to its idle, low power state.
  3811. */
  3812. WARN_ON(dev_priv->gt.awake);
  3813. WARN_ON(!intel_engines_are_idle(dev_priv));
  3814. /*
  3815. * Neither the BIOS, ourselves or any other kernel
  3816. * expects the system to be in execlists mode on startup,
  3817. * so we need to reset the GPU back to legacy mode. And the only
  3818. * known way to disable logical contexts is through a GPU reset.
  3819. *
  3820. * So in order to leave the system in a known default configuration,
  3821. * always reset the GPU upon unload and suspend. Afterwards we then
  3822. * clean up the GEM state tracking, flushing off the requests and
  3823. * leaving the system in a known idle state.
  3824. *
  3825. * Note that is of the upmost importance that the GPU is idle and
  3826. * all stray writes are flushed *before* we dismantle the backing
  3827. * storage for the pinned objects.
  3828. *
  3829. * However, since we are uncertain that resetting the GPU on older
  3830. * machines is a good idea, we don't - just in case it leaves the
  3831. * machine in an unusable condition.
  3832. */
  3833. i915_gem_sanitize(dev_priv);
  3834. goto out_rpm_put;
  3835. err_unlock:
  3836. mutex_unlock(&dev->struct_mutex);
  3837. out_rpm_put:
  3838. intel_runtime_pm_put(dev_priv);
  3839. return ret;
  3840. }
  3841. void i915_gem_resume(struct drm_i915_private *dev_priv)
  3842. {
  3843. struct drm_device *dev = &dev_priv->drm;
  3844. WARN_ON(dev_priv->gt.awake);
  3845. mutex_lock(&dev->struct_mutex);
  3846. i915_gem_restore_gtt_mappings(dev_priv);
  3847. /* As we didn't flush the kernel context before suspend, we cannot
  3848. * guarantee that the context image is complete. So let's just reset
  3849. * it and start again.
  3850. */
  3851. dev_priv->gt.resume(dev_priv);
  3852. mutex_unlock(&dev->struct_mutex);
  3853. }
  3854. void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
  3855. {
  3856. if (INTEL_GEN(dev_priv) < 5 ||
  3857. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  3858. return;
  3859. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  3860. DISP_TILE_SURFACE_SWIZZLING);
  3861. if (IS_GEN5(dev_priv))
  3862. return;
  3863. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  3864. if (IS_GEN6(dev_priv))
  3865. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  3866. else if (IS_GEN7(dev_priv))
  3867. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  3868. else if (IS_GEN8(dev_priv))
  3869. I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
  3870. else
  3871. BUG();
  3872. }
  3873. static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
  3874. {
  3875. I915_WRITE(RING_CTL(base), 0);
  3876. I915_WRITE(RING_HEAD(base), 0);
  3877. I915_WRITE(RING_TAIL(base), 0);
  3878. I915_WRITE(RING_START(base), 0);
  3879. }
  3880. static void init_unused_rings(struct drm_i915_private *dev_priv)
  3881. {
  3882. if (IS_I830(dev_priv)) {
  3883. init_unused_ring(dev_priv, PRB1_BASE);
  3884. init_unused_ring(dev_priv, SRB0_BASE);
  3885. init_unused_ring(dev_priv, SRB1_BASE);
  3886. init_unused_ring(dev_priv, SRB2_BASE);
  3887. init_unused_ring(dev_priv, SRB3_BASE);
  3888. } else if (IS_GEN2(dev_priv)) {
  3889. init_unused_ring(dev_priv, SRB0_BASE);
  3890. init_unused_ring(dev_priv, SRB1_BASE);
  3891. } else if (IS_GEN3(dev_priv)) {
  3892. init_unused_ring(dev_priv, PRB1_BASE);
  3893. init_unused_ring(dev_priv, PRB2_BASE);
  3894. }
  3895. }
  3896. static int __i915_gem_restart_engines(void *data)
  3897. {
  3898. struct drm_i915_private *i915 = data;
  3899. struct intel_engine_cs *engine;
  3900. enum intel_engine_id id;
  3901. int err;
  3902. for_each_engine(engine, i915, id) {
  3903. err = engine->init_hw(engine);
  3904. if (err)
  3905. return err;
  3906. }
  3907. return 0;
  3908. }
  3909. int i915_gem_init_hw(struct drm_i915_private *dev_priv)
  3910. {
  3911. int ret;
  3912. dev_priv->gt.last_init_time = ktime_get();
  3913. /* Double layer security blanket, see i915_gem_init() */
  3914. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  3915. if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
  3916. I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  3917. if (IS_HASWELL(dev_priv))
  3918. I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
  3919. LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
  3920. if (HAS_PCH_NOP(dev_priv)) {
  3921. if (IS_IVYBRIDGE(dev_priv)) {
  3922. u32 temp = I915_READ(GEN7_MSG_CTL);
  3923. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  3924. I915_WRITE(GEN7_MSG_CTL, temp);
  3925. } else if (INTEL_GEN(dev_priv) >= 7) {
  3926. u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
  3927. temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
  3928. I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
  3929. }
  3930. }
  3931. i915_gem_init_swizzling(dev_priv);
  3932. /*
  3933. * At least 830 can leave some of the unused rings
  3934. * "active" (ie. head != tail) after resume which
  3935. * will prevent c3 entry. Makes sure all unused rings
  3936. * are totally idle.
  3937. */
  3938. init_unused_rings(dev_priv);
  3939. BUG_ON(!dev_priv->kernel_context);
  3940. ret = i915_ppgtt_init_hw(dev_priv);
  3941. if (ret) {
  3942. DRM_ERROR("PPGTT enable HW failed %d\n", ret);
  3943. goto out;
  3944. }
  3945. /* Need to do basic initialisation of all rings first: */
  3946. ret = __i915_gem_restart_engines(dev_priv);
  3947. if (ret)
  3948. goto out;
  3949. intel_mocs_init_l3cc_table(dev_priv);
  3950. /* We can't enable contexts until all firmware is loaded */
  3951. ret = intel_uc_init_hw(dev_priv);
  3952. if (ret)
  3953. goto out;
  3954. out:
  3955. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  3956. return ret;
  3957. }
  3958. bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value)
  3959. {
  3960. if (INTEL_INFO(dev_priv)->gen < 6)
  3961. return false;
  3962. /* TODO: make semaphores and Execlists play nicely together */
  3963. if (i915.enable_execlists)
  3964. return false;
  3965. if (value >= 0)
  3966. return value;
  3967. #ifdef CONFIG_INTEL_IOMMU
  3968. /* Enable semaphores on SNB when IO remapping is off */
  3969. if (INTEL_INFO(dev_priv)->gen == 6 && intel_iommu_gfx_mapped)
  3970. return false;
  3971. #endif
  3972. return true;
  3973. }
  3974. int i915_gem_init(struct drm_i915_private *dev_priv)
  3975. {
  3976. int ret;
  3977. mutex_lock(&dev_priv->drm.struct_mutex);
  3978. dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
  3979. if (!i915.enable_execlists) {
  3980. dev_priv->gt.resume = intel_legacy_submission_resume;
  3981. dev_priv->gt.cleanup_engine = intel_engine_cleanup;
  3982. } else {
  3983. dev_priv->gt.resume = intel_lr_context_resume;
  3984. dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
  3985. }
  3986. /* This is just a security blanket to placate dragons.
  3987. * On some systems, we very sporadically observe that the first TLBs
  3988. * used by the CS may be stale, despite us poking the TLB reset. If
  3989. * we hold the forcewake during initialisation these problems
  3990. * just magically go away.
  3991. */
  3992. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  3993. i915_gem_init_userptr(dev_priv);
  3994. ret = i915_gem_init_ggtt(dev_priv);
  3995. if (ret)
  3996. goto out_unlock;
  3997. ret = i915_gem_context_init(dev_priv);
  3998. if (ret)
  3999. goto out_unlock;
  4000. ret = intel_engines_init(dev_priv);
  4001. if (ret)
  4002. goto out_unlock;
  4003. ret = i915_gem_init_hw(dev_priv);
  4004. if (ret == -EIO) {
  4005. /* Allow engine initialisation to fail by marking the GPU as
  4006. * wedged. But we only want to do this where the GPU is angry,
  4007. * for all other failure, such as an allocation failure, bail.
  4008. */
  4009. DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
  4010. i915_gem_set_wedged(dev_priv);
  4011. ret = 0;
  4012. }
  4013. out_unlock:
  4014. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4015. mutex_unlock(&dev_priv->drm.struct_mutex);
  4016. return ret;
  4017. }
  4018. void i915_gem_init_mmio(struct drm_i915_private *i915)
  4019. {
  4020. i915_gem_sanitize(i915);
  4021. }
  4022. void
  4023. i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
  4024. {
  4025. struct intel_engine_cs *engine;
  4026. enum intel_engine_id id;
  4027. for_each_engine(engine, dev_priv, id)
  4028. dev_priv->gt.cleanup_engine(engine);
  4029. }
  4030. void
  4031. i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
  4032. {
  4033. int i;
  4034. if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
  4035. !IS_CHERRYVIEW(dev_priv))
  4036. dev_priv->num_fence_regs = 32;
  4037. else if (INTEL_INFO(dev_priv)->gen >= 4 ||
  4038. IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  4039. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
  4040. dev_priv->num_fence_regs = 16;
  4041. else
  4042. dev_priv->num_fence_regs = 8;
  4043. if (intel_vgpu_active(dev_priv))
  4044. dev_priv->num_fence_regs =
  4045. I915_READ(vgtif_reg(avail_rs.fence_num));
  4046. /* Initialize fence registers to zero */
  4047. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  4048. struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
  4049. fence->i915 = dev_priv;
  4050. fence->id = i;
  4051. list_add_tail(&fence->link, &dev_priv->mm.fence_list);
  4052. }
  4053. i915_gem_restore_fences(dev_priv);
  4054. i915_gem_detect_bit_6_swizzle(dev_priv);
  4055. }
  4056. int
  4057. i915_gem_load_init(struct drm_i915_private *dev_priv)
  4058. {
  4059. int err = -ENOMEM;
  4060. dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
  4061. if (!dev_priv->objects)
  4062. goto err_out;
  4063. dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
  4064. if (!dev_priv->vmas)
  4065. goto err_objects;
  4066. dev_priv->requests = KMEM_CACHE(drm_i915_gem_request,
  4067. SLAB_HWCACHE_ALIGN |
  4068. SLAB_RECLAIM_ACCOUNT |
  4069. SLAB_DESTROY_BY_RCU);
  4070. if (!dev_priv->requests)
  4071. goto err_vmas;
  4072. dev_priv->dependencies = KMEM_CACHE(i915_dependency,
  4073. SLAB_HWCACHE_ALIGN |
  4074. SLAB_RECLAIM_ACCOUNT);
  4075. if (!dev_priv->dependencies)
  4076. goto err_requests;
  4077. mutex_lock(&dev_priv->drm.struct_mutex);
  4078. INIT_LIST_HEAD(&dev_priv->gt.timelines);
  4079. err = i915_gem_timeline_init__global(dev_priv);
  4080. mutex_unlock(&dev_priv->drm.struct_mutex);
  4081. if (err)
  4082. goto err_dependencies;
  4083. INIT_LIST_HEAD(&dev_priv->context_list);
  4084. INIT_WORK(&dev_priv->mm.free_work, __i915_gem_free_work);
  4085. init_llist_head(&dev_priv->mm.free_list);
  4086. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  4087. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  4088. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4089. INIT_LIST_HEAD(&dev_priv->mm.userfault_list);
  4090. INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
  4091. i915_gem_retire_work_handler);
  4092. INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
  4093. i915_gem_idle_work_handler);
  4094. init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
  4095. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  4096. init_waitqueue_head(&dev_priv->pending_flip_queue);
  4097. atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
  4098. spin_lock_init(&dev_priv->fb_tracking.lock);
  4099. return 0;
  4100. err_dependencies:
  4101. kmem_cache_destroy(dev_priv->dependencies);
  4102. err_requests:
  4103. kmem_cache_destroy(dev_priv->requests);
  4104. err_vmas:
  4105. kmem_cache_destroy(dev_priv->vmas);
  4106. err_objects:
  4107. kmem_cache_destroy(dev_priv->objects);
  4108. err_out:
  4109. return err;
  4110. }
  4111. void i915_gem_load_cleanup(struct drm_i915_private *dev_priv)
  4112. {
  4113. i915_gem_drain_freed_objects(dev_priv);
  4114. WARN_ON(!llist_empty(&dev_priv->mm.free_list));
  4115. WARN_ON(dev_priv->mm.object_count);
  4116. mutex_lock(&dev_priv->drm.struct_mutex);
  4117. i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
  4118. WARN_ON(!list_empty(&dev_priv->gt.timelines));
  4119. mutex_unlock(&dev_priv->drm.struct_mutex);
  4120. kmem_cache_destroy(dev_priv->dependencies);
  4121. kmem_cache_destroy(dev_priv->requests);
  4122. kmem_cache_destroy(dev_priv->vmas);
  4123. kmem_cache_destroy(dev_priv->objects);
  4124. /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
  4125. rcu_barrier();
  4126. }
  4127. int i915_gem_freeze(struct drm_i915_private *dev_priv)
  4128. {
  4129. /* Discard all purgeable objects, let userspace recover those as
  4130. * required after resuming.
  4131. */
  4132. i915_gem_shrink_all(dev_priv);
  4133. return 0;
  4134. }
  4135. int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
  4136. {
  4137. struct drm_i915_gem_object *obj;
  4138. struct list_head *phases[] = {
  4139. &dev_priv->mm.unbound_list,
  4140. &dev_priv->mm.bound_list,
  4141. NULL
  4142. }, **p;
  4143. /* Called just before we write the hibernation image.
  4144. *
  4145. * We need to update the domain tracking to reflect that the CPU
  4146. * will be accessing all the pages to create and restore from the
  4147. * hibernation, and so upon restoration those pages will be in the
  4148. * CPU domain.
  4149. *
  4150. * To make sure the hibernation image contains the latest state,
  4151. * we update that state just before writing out the image.
  4152. *
  4153. * To try and reduce the hibernation image, we manually shrink
  4154. * the objects as well, see i915_gem_freeze()
  4155. */
  4156. i915_gem_shrink(dev_priv, -1UL, I915_SHRINK_UNBOUND);
  4157. i915_gem_drain_freed_objects(dev_priv);
  4158. mutex_lock(&dev_priv->drm.struct_mutex);
  4159. for (p = phases; *p; p++) {
  4160. list_for_each_entry(obj, *p, global_link) {
  4161. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  4162. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  4163. }
  4164. }
  4165. mutex_unlock(&dev_priv->drm.struct_mutex);
  4166. return 0;
  4167. }
  4168. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  4169. {
  4170. struct drm_i915_file_private *file_priv = file->driver_priv;
  4171. struct drm_i915_gem_request *request;
  4172. /* Clean up our request list when the client is going away, so that
  4173. * later retire_requests won't dereference our soon-to-be-gone
  4174. * file_priv.
  4175. */
  4176. spin_lock(&file_priv->mm.lock);
  4177. list_for_each_entry(request, &file_priv->mm.request_list, client_link)
  4178. request->file_priv = NULL;
  4179. spin_unlock(&file_priv->mm.lock);
  4180. if (!list_empty(&file_priv->rps.link)) {
  4181. spin_lock(&to_i915(dev)->rps.client_lock);
  4182. list_del(&file_priv->rps.link);
  4183. spin_unlock(&to_i915(dev)->rps.client_lock);
  4184. }
  4185. }
  4186. int i915_gem_open(struct drm_device *dev, struct drm_file *file)
  4187. {
  4188. struct drm_i915_file_private *file_priv;
  4189. int ret;
  4190. DRM_DEBUG("\n");
  4191. file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
  4192. if (!file_priv)
  4193. return -ENOMEM;
  4194. file->driver_priv = file_priv;
  4195. file_priv->dev_priv = to_i915(dev);
  4196. file_priv->file = file;
  4197. INIT_LIST_HEAD(&file_priv->rps.link);
  4198. spin_lock_init(&file_priv->mm.lock);
  4199. INIT_LIST_HEAD(&file_priv->mm.request_list);
  4200. file_priv->bsd_engine = -1;
  4201. ret = i915_gem_context_open(dev, file);
  4202. if (ret)
  4203. kfree(file_priv);
  4204. return ret;
  4205. }
  4206. /**
  4207. * i915_gem_track_fb - update frontbuffer tracking
  4208. * @old: current GEM buffer for the frontbuffer slots
  4209. * @new: new GEM buffer for the frontbuffer slots
  4210. * @frontbuffer_bits: bitmask of frontbuffer slots
  4211. *
  4212. * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
  4213. * from @old and setting them in @new. Both @old and @new can be NULL.
  4214. */
  4215. void i915_gem_track_fb(struct drm_i915_gem_object *old,
  4216. struct drm_i915_gem_object *new,
  4217. unsigned frontbuffer_bits)
  4218. {
  4219. /* Control of individual bits within the mask are guarded by
  4220. * the owning plane->mutex, i.e. we can never see concurrent
  4221. * manipulation of individual bits. But since the bitfield as a whole
  4222. * is updated using RMW, we need to use atomics in order to update
  4223. * the bits.
  4224. */
  4225. BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
  4226. sizeof(atomic_t) * BITS_PER_BYTE);
  4227. if (old) {
  4228. WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
  4229. atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
  4230. }
  4231. if (new) {
  4232. WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
  4233. atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
  4234. }
  4235. }
  4236. /* Allocate a new GEM object and fill it with the supplied data */
  4237. struct drm_i915_gem_object *
  4238. i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
  4239. const void *data, size_t size)
  4240. {
  4241. struct drm_i915_gem_object *obj;
  4242. struct file *file;
  4243. size_t offset;
  4244. int err;
  4245. obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
  4246. if (IS_ERR(obj))
  4247. return obj;
  4248. GEM_BUG_ON(obj->base.write_domain != I915_GEM_DOMAIN_CPU);
  4249. file = obj->base.filp;
  4250. offset = 0;
  4251. do {
  4252. unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
  4253. struct page *page;
  4254. void *pgdata, *vaddr;
  4255. err = pagecache_write_begin(file, file->f_mapping,
  4256. offset, len, 0,
  4257. &page, &pgdata);
  4258. if (err < 0)
  4259. goto fail;
  4260. vaddr = kmap(page);
  4261. memcpy(vaddr, data, len);
  4262. kunmap(page);
  4263. err = pagecache_write_end(file, file->f_mapping,
  4264. offset, len, len,
  4265. page, pgdata);
  4266. if (err < 0)
  4267. goto fail;
  4268. size -= len;
  4269. data += len;
  4270. offset += len;
  4271. } while (size);
  4272. return obj;
  4273. fail:
  4274. i915_gem_object_put(obj);
  4275. return ERR_PTR(err);
  4276. }
  4277. struct scatterlist *
  4278. i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
  4279. unsigned int n,
  4280. unsigned int *offset)
  4281. {
  4282. struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
  4283. struct scatterlist *sg;
  4284. unsigned int idx, count;
  4285. might_sleep();
  4286. GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
  4287. GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
  4288. /* As we iterate forward through the sg, we record each entry in a
  4289. * radixtree for quick repeated (backwards) lookups. If we have seen
  4290. * this index previously, we will have an entry for it.
  4291. *
  4292. * Initial lookup is O(N), but this is amortized to O(1) for
  4293. * sequential page access (where each new request is consecutive
  4294. * to the previous one). Repeated lookups are O(lg(obj->base.size)),
  4295. * i.e. O(1) with a large constant!
  4296. */
  4297. if (n < READ_ONCE(iter->sg_idx))
  4298. goto lookup;
  4299. mutex_lock(&iter->lock);
  4300. /* We prefer to reuse the last sg so that repeated lookup of this
  4301. * (or the subsequent) sg are fast - comparing against the last
  4302. * sg is faster than going through the radixtree.
  4303. */
  4304. sg = iter->sg_pos;
  4305. idx = iter->sg_idx;
  4306. count = __sg_page_count(sg);
  4307. while (idx + count <= n) {
  4308. unsigned long exception, i;
  4309. int ret;
  4310. /* If we cannot allocate and insert this entry, or the
  4311. * individual pages from this range, cancel updating the
  4312. * sg_idx so that on this lookup we are forced to linearly
  4313. * scan onwards, but on future lookups we will try the
  4314. * insertion again (in which case we need to be careful of
  4315. * the error return reporting that we have already inserted
  4316. * this index).
  4317. */
  4318. ret = radix_tree_insert(&iter->radix, idx, sg);
  4319. if (ret && ret != -EEXIST)
  4320. goto scan;
  4321. exception =
  4322. RADIX_TREE_EXCEPTIONAL_ENTRY |
  4323. idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
  4324. for (i = 1; i < count; i++) {
  4325. ret = radix_tree_insert(&iter->radix, idx + i,
  4326. (void *)exception);
  4327. if (ret && ret != -EEXIST)
  4328. goto scan;
  4329. }
  4330. idx += count;
  4331. sg = ____sg_next(sg);
  4332. count = __sg_page_count(sg);
  4333. }
  4334. scan:
  4335. iter->sg_pos = sg;
  4336. iter->sg_idx = idx;
  4337. mutex_unlock(&iter->lock);
  4338. if (unlikely(n < idx)) /* insertion completed by another thread */
  4339. goto lookup;
  4340. /* In case we failed to insert the entry into the radixtree, we need
  4341. * to look beyond the current sg.
  4342. */
  4343. while (idx + count <= n) {
  4344. idx += count;
  4345. sg = ____sg_next(sg);
  4346. count = __sg_page_count(sg);
  4347. }
  4348. *offset = n - idx;
  4349. return sg;
  4350. lookup:
  4351. rcu_read_lock();
  4352. sg = radix_tree_lookup(&iter->radix, n);
  4353. GEM_BUG_ON(!sg);
  4354. /* If this index is in the middle of multi-page sg entry,
  4355. * the radixtree will contain an exceptional entry that points
  4356. * to the start of that range. We will return the pointer to
  4357. * the base page and the offset of this page within the
  4358. * sg entry's range.
  4359. */
  4360. *offset = 0;
  4361. if (unlikely(radix_tree_exception(sg))) {
  4362. unsigned long base =
  4363. (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
  4364. sg = radix_tree_lookup(&iter->radix, base);
  4365. GEM_BUG_ON(!sg);
  4366. *offset = n - base;
  4367. }
  4368. rcu_read_unlock();
  4369. return sg;
  4370. }
  4371. struct page *
  4372. i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
  4373. {
  4374. struct scatterlist *sg;
  4375. unsigned int offset;
  4376. GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
  4377. sg = i915_gem_object_get_sg(obj, n, &offset);
  4378. return nth_page(sg_page(sg), offset);
  4379. }
  4380. /* Like i915_gem_object_get_page(), but mark the returned page dirty */
  4381. struct page *
  4382. i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
  4383. unsigned int n)
  4384. {
  4385. struct page *page;
  4386. page = i915_gem_object_get_page(obj, n);
  4387. if (!obj->mm.dirty)
  4388. set_page_dirty(page);
  4389. return page;
  4390. }
  4391. dma_addr_t
  4392. i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
  4393. unsigned long n)
  4394. {
  4395. struct scatterlist *sg;
  4396. unsigned int offset;
  4397. sg = i915_gem_object_get_sg(obj, n, &offset);
  4398. return sg_dma_address(sg) + (offset << PAGE_SHIFT);
  4399. }
  4400. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  4401. #include "selftests/scatterlist.c"
  4402. #include "selftests/mock_gem_device.c"
  4403. #include "selftests/huge_gem_object.c"
  4404. #include "selftests/i915_gem_object.c"
  4405. #include "selftests/i915_gem_coherency.c"
  4406. #endif