i915_gem.c 132 KB

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