i915_gem.c 139 KB

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