i915_gem.c 126 KB

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