i915_gem.c 126 KB

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