i915_gem.c 144 KB

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