i915_gem.c 135 KB

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