i915_gem.c 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400
  1. /*
  2. * Copyright © 2008 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/i915_drm.h>
  29. #include "i915_drv.h"
  30. #include "i915_trace.h"
  31. #include "intel_drv.h"
  32. #include <linux/shmem_fs.h>
  33. #include <linux/slab.h>
  34. #include <linux/swap.h>
  35. #include <linux/pci.h>
  36. #include <linux/dma-buf.h>
  37. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  38. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
  39. static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
  40. unsigned alignment,
  41. bool map_and_fenceable,
  42. bool nonblocking);
  43. static int i915_gem_phys_pwrite(struct drm_device *dev,
  44. struct drm_i915_gem_object *obj,
  45. struct drm_i915_gem_pwrite *args,
  46. struct drm_file *file);
  47. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  48. struct drm_i915_gem_object *obj);
  49. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  50. struct drm_i915_fence_reg *fence,
  51. bool enable);
  52. static int i915_gem_inactive_shrink(struct shrinker *shrinker,
  53. struct shrink_control *sc);
  54. static long i915_gem_purge(struct drm_i915_private *dev_priv, long target);
  55. static void i915_gem_shrink_all(struct drm_i915_private *dev_priv);
  56. static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
  57. static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
  58. {
  59. if (obj->tiling_mode)
  60. i915_gem_release_mmap(obj);
  61. /* As we do not have an associated fence register, we will force
  62. * a tiling change if we ever need to acquire one.
  63. */
  64. obj->fence_dirty = false;
  65. obj->fence_reg = I915_FENCE_REG_NONE;
  66. }
  67. /* some bookkeeping */
  68. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  69. size_t size)
  70. {
  71. dev_priv->mm.object_count++;
  72. dev_priv->mm.object_memory += size;
  73. }
  74. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  75. size_t size)
  76. {
  77. dev_priv->mm.object_count--;
  78. dev_priv->mm.object_memory -= size;
  79. }
  80. static int
  81. i915_gem_wait_for_error(struct drm_device *dev)
  82. {
  83. struct drm_i915_private *dev_priv = dev->dev_private;
  84. struct completion *x = &dev_priv->error_completion;
  85. unsigned long flags;
  86. int ret;
  87. if (!atomic_read(&dev_priv->mm.wedged))
  88. return 0;
  89. /*
  90. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  91. * userspace. If it takes that long something really bad is going on and
  92. * we should simply try to bail out and fail as gracefully as possible.
  93. */
  94. ret = wait_for_completion_interruptible_timeout(x, 10*HZ);
  95. if (ret == 0) {
  96. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  97. return -EIO;
  98. } else if (ret < 0) {
  99. return ret;
  100. }
  101. if (atomic_read(&dev_priv->mm.wedged)) {
  102. /* GPU is hung, bump the completion count to account for
  103. * the token we just consumed so that we never hit zero and
  104. * end up waiting upon a subsequent completion event that
  105. * will never happen.
  106. */
  107. spin_lock_irqsave(&x->wait.lock, flags);
  108. x->done++;
  109. spin_unlock_irqrestore(&x->wait.lock, flags);
  110. }
  111. return 0;
  112. }
  113. int i915_mutex_lock_interruptible(struct drm_device *dev)
  114. {
  115. int ret;
  116. ret = i915_gem_wait_for_error(dev);
  117. if (ret)
  118. return ret;
  119. ret = mutex_lock_interruptible(&dev->struct_mutex);
  120. if (ret)
  121. return ret;
  122. WARN_ON(i915_verify_lists(dev));
  123. return 0;
  124. }
  125. static inline bool
  126. i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
  127. {
  128. return obj->gtt_space && !obj->active;
  129. }
  130. int
  131. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  132. struct drm_file *file)
  133. {
  134. struct drm_i915_gem_init *args = data;
  135. if (drm_core_check_feature(dev, DRIVER_MODESET))
  136. return -ENODEV;
  137. if (args->gtt_start >= args->gtt_end ||
  138. (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
  139. return -EINVAL;
  140. /* GEM with user mode setting was never supported on ilk and later. */
  141. if (INTEL_INFO(dev)->gen >= 5)
  142. return -ENODEV;
  143. mutex_lock(&dev->struct_mutex);
  144. i915_gem_init_global_gtt(dev, args->gtt_start,
  145. args->gtt_end, args->gtt_end);
  146. mutex_unlock(&dev->struct_mutex);
  147. return 0;
  148. }
  149. int
  150. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  151. struct drm_file *file)
  152. {
  153. struct drm_i915_private *dev_priv = dev->dev_private;
  154. struct drm_i915_gem_get_aperture *args = data;
  155. struct drm_i915_gem_object *obj;
  156. size_t pinned;
  157. pinned = 0;
  158. mutex_lock(&dev->struct_mutex);
  159. list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
  160. if (obj->pin_count)
  161. pinned += obj->gtt_space->size;
  162. mutex_unlock(&dev->struct_mutex);
  163. args->aper_size = dev_priv->mm.gtt_total;
  164. args->aper_available_size = args->aper_size - pinned;
  165. return 0;
  166. }
  167. static int
  168. i915_gem_create(struct drm_file *file,
  169. struct drm_device *dev,
  170. uint64_t size,
  171. uint32_t *handle_p)
  172. {
  173. struct drm_i915_gem_object *obj;
  174. int ret;
  175. u32 handle;
  176. size = roundup(size, PAGE_SIZE);
  177. if (size == 0)
  178. return -EINVAL;
  179. /* Allocate the new object */
  180. obj = i915_gem_alloc_object(dev, size);
  181. if (obj == NULL)
  182. return -ENOMEM;
  183. ret = drm_gem_handle_create(file, &obj->base, &handle);
  184. if (ret) {
  185. drm_gem_object_release(&obj->base);
  186. i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
  187. kfree(obj);
  188. return ret;
  189. }
  190. /* drop reference from allocate - handle holds it now */
  191. drm_gem_object_unreference(&obj->base);
  192. trace_i915_gem_object_create(obj);
  193. *handle_p = handle;
  194. return 0;
  195. }
  196. int
  197. i915_gem_dumb_create(struct drm_file *file,
  198. struct drm_device *dev,
  199. struct drm_mode_create_dumb *args)
  200. {
  201. /* have to work out size/pitch and return them */
  202. args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
  203. args->size = args->pitch * args->height;
  204. return i915_gem_create(file, dev,
  205. args->size, &args->handle);
  206. }
  207. int i915_gem_dumb_destroy(struct drm_file *file,
  208. struct drm_device *dev,
  209. uint32_t handle)
  210. {
  211. return drm_gem_handle_delete(file, handle);
  212. }
  213. /**
  214. * Creates a new mm object and returns a handle to it.
  215. */
  216. int
  217. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  218. struct drm_file *file)
  219. {
  220. struct drm_i915_gem_create *args = data;
  221. return i915_gem_create(file, dev,
  222. args->size, &args->handle);
  223. }
  224. static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
  225. {
  226. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  227. return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
  228. obj->tiling_mode != I915_TILING_NONE;
  229. }
  230. static inline int
  231. __copy_to_user_swizzled(char __user *cpu_vaddr,
  232. const char *gpu_vaddr, int gpu_offset,
  233. int length)
  234. {
  235. int ret, cpu_offset = 0;
  236. while (length > 0) {
  237. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  238. int this_length = min(cacheline_end - gpu_offset, length);
  239. int swizzled_gpu_offset = gpu_offset ^ 64;
  240. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  241. gpu_vaddr + swizzled_gpu_offset,
  242. this_length);
  243. if (ret)
  244. return ret + length;
  245. cpu_offset += this_length;
  246. gpu_offset += this_length;
  247. length -= this_length;
  248. }
  249. return 0;
  250. }
  251. static inline int
  252. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  253. const char __user *cpu_vaddr,
  254. int length)
  255. {
  256. int ret, cpu_offset = 0;
  257. while (length > 0) {
  258. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  259. int this_length = min(cacheline_end - gpu_offset, length);
  260. int swizzled_gpu_offset = gpu_offset ^ 64;
  261. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  262. cpu_vaddr + cpu_offset,
  263. this_length);
  264. if (ret)
  265. return ret + length;
  266. cpu_offset += this_length;
  267. gpu_offset += this_length;
  268. length -= this_length;
  269. }
  270. return 0;
  271. }
  272. /* Per-page copy function for the shmem pread fastpath.
  273. * Flushes invalid cachelines before reading the target if
  274. * needs_clflush is set. */
  275. static int
  276. shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
  277. char __user *user_data,
  278. bool page_do_bit17_swizzling, bool needs_clflush)
  279. {
  280. char *vaddr;
  281. int ret;
  282. if (unlikely(page_do_bit17_swizzling))
  283. return -EINVAL;
  284. vaddr = kmap_atomic(page);
  285. if (needs_clflush)
  286. drm_clflush_virt_range(vaddr + shmem_page_offset,
  287. page_length);
  288. ret = __copy_to_user_inatomic(user_data,
  289. vaddr + shmem_page_offset,
  290. page_length);
  291. kunmap_atomic(vaddr);
  292. return ret ? -EFAULT : 0;
  293. }
  294. static void
  295. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  296. bool swizzled)
  297. {
  298. if (unlikely(swizzled)) {
  299. unsigned long start = (unsigned long) addr;
  300. unsigned long end = (unsigned long) addr + length;
  301. /* For swizzling simply ensure that we always flush both
  302. * channels. Lame, but simple and it works. Swizzled
  303. * pwrite/pread is far from a hotpath - current userspace
  304. * doesn't use it at all. */
  305. start = round_down(start, 128);
  306. end = round_up(end, 128);
  307. drm_clflush_virt_range((void *)start, end - start);
  308. } else {
  309. drm_clflush_virt_range(addr, length);
  310. }
  311. }
  312. /* Only difference to the fast-path function is that this can handle bit17
  313. * and uses non-atomic copy and kmap functions. */
  314. static int
  315. shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
  316. char __user *user_data,
  317. bool page_do_bit17_swizzling, bool needs_clflush)
  318. {
  319. char *vaddr;
  320. int ret;
  321. vaddr = kmap(page);
  322. if (needs_clflush)
  323. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  324. page_length,
  325. page_do_bit17_swizzling);
  326. if (page_do_bit17_swizzling)
  327. ret = __copy_to_user_swizzled(user_data,
  328. vaddr, shmem_page_offset,
  329. page_length);
  330. else
  331. ret = __copy_to_user(user_data,
  332. vaddr + shmem_page_offset,
  333. page_length);
  334. kunmap(page);
  335. return ret ? - EFAULT : 0;
  336. }
  337. static int
  338. i915_gem_shmem_pread(struct drm_device *dev,
  339. struct drm_i915_gem_object *obj,
  340. struct drm_i915_gem_pread *args,
  341. struct drm_file *file)
  342. {
  343. char __user *user_data;
  344. ssize_t remain;
  345. loff_t offset;
  346. int shmem_page_offset, page_length, ret = 0;
  347. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  348. int prefaulted = 0;
  349. int needs_clflush = 0;
  350. struct scatterlist *sg;
  351. int i;
  352. user_data = (char __user *) (uintptr_t) args->data_ptr;
  353. remain = args->size;
  354. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  355. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
  356. /* If we're not in the cpu read domain, set ourself into the gtt
  357. * read domain and manually flush cachelines (if required). This
  358. * optimizes for the case when the gpu will dirty the data
  359. * anyway again before the next pread happens. */
  360. if (obj->cache_level == I915_CACHE_NONE)
  361. needs_clflush = 1;
  362. if (obj->gtt_space) {
  363. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  364. if (ret)
  365. return ret;
  366. }
  367. }
  368. ret = i915_gem_object_get_pages(obj);
  369. if (ret)
  370. return ret;
  371. i915_gem_object_pin_pages(obj);
  372. offset = args->offset;
  373. for_each_sg(obj->pages->sgl, sg, obj->pages->nents, i) {
  374. struct page *page;
  375. if (i < offset >> PAGE_SHIFT)
  376. continue;
  377. if (remain <= 0)
  378. break;
  379. /* Operation in this page
  380. *
  381. * shmem_page_offset = offset within page in shmem file
  382. * page_length = bytes to copy for this page
  383. */
  384. shmem_page_offset = offset_in_page(offset);
  385. page_length = remain;
  386. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  387. page_length = PAGE_SIZE - shmem_page_offset;
  388. page = sg_page(sg);
  389. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  390. (page_to_phys(page) & (1 << 17)) != 0;
  391. ret = shmem_pread_fast(page, shmem_page_offset, page_length,
  392. user_data, page_do_bit17_swizzling,
  393. needs_clflush);
  394. if (ret == 0)
  395. goto next_page;
  396. mutex_unlock(&dev->struct_mutex);
  397. if (!prefaulted) {
  398. ret = fault_in_multipages_writeable(user_data, remain);
  399. /* Userspace is tricking us, but we've already clobbered
  400. * its pages with the prefault and promised to write the
  401. * data up to the first fault. Hence ignore any errors
  402. * and just continue. */
  403. (void)ret;
  404. prefaulted = 1;
  405. }
  406. ret = shmem_pread_slow(page, shmem_page_offset, page_length,
  407. user_data, page_do_bit17_swizzling,
  408. needs_clflush);
  409. mutex_lock(&dev->struct_mutex);
  410. next_page:
  411. mark_page_accessed(page);
  412. if (ret)
  413. goto out;
  414. remain -= page_length;
  415. user_data += page_length;
  416. offset += page_length;
  417. }
  418. out:
  419. i915_gem_object_unpin_pages(obj);
  420. return ret;
  421. }
  422. /**
  423. * Reads data from the object referenced by handle.
  424. *
  425. * On error, the contents of *data are undefined.
  426. */
  427. int
  428. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  429. struct drm_file *file)
  430. {
  431. struct drm_i915_gem_pread *args = data;
  432. struct drm_i915_gem_object *obj;
  433. int ret = 0;
  434. if (args->size == 0)
  435. return 0;
  436. if (!access_ok(VERIFY_WRITE,
  437. (char __user *)(uintptr_t)args->data_ptr,
  438. args->size))
  439. return -EFAULT;
  440. ret = i915_mutex_lock_interruptible(dev);
  441. if (ret)
  442. return ret;
  443. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  444. if (&obj->base == NULL) {
  445. ret = -ENOENT;
  446. goto unlock;
  447. }
  448. /* Bounds check source. */
  449. if (args->offset > obj->base.size ||
  450. args->size > obj->base.size - args->offset) {
  451. ret = -EINVAL;
  452. goto out;
  453. }
  454. /* prime objects have no backing filp to GEM pread/pwrite
  455. * pages from.
  456. */
  457. if (!obj->base.filp) {
  458. ret = -EINVAL;
  459. goto out;
  460. }
  461. trace_i915_gem_object_pread(obj, args->offset, args->size);
  462. ret = i915_gem_shmem_pread(dev, obj, args, file);
  463. out:
  464. drm_gem_object_unreference(&obj->base);
  465. unlock:
  466. mutex_unlock(&dev->struct_mutex);
  467. return ret;
  468. }
  469. /* This is the fast write path which cannot handle
  470. * page faults in the source data
  471. */
  472. static inline int
  473. fast_user_write(struct io_mapping *mapping,
  474. loff_t page_base, int page_offset,
  475. char __user *user_data,
  476. int length)
  477. {
  478. void __iomem *vaddr_atomic;
  479. void *vaddr;
  480. unsigned long unwritten;
  481. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  482. /* We can use the cpu mem copy function because this is X86. */
  483. vaddr = (void __force*)vaddr_atomic + page_offset;
  484. unwritten = __copy_from_user_inatomic_nocache(vaddr,
  485. user_data, length);
  486. io_mapping_unmap_atomic(vaddr_atomic);
  487. return unwritten;
  488. }
  489. /**
  490. * This is the fast pwrite path, where we copy the data directly from the
  491. * user into the GTT, uncached.
  492. */
  493. static int
  494. i915_gem_gtt_pwrite_fast(struct drm_device *dev,
  495. struct drm_i915_gem_object *obj,
  496. struct drm_i915_gem_pwrite *args,
  497. struct drm_file *file)
  498. {
  499. drm_i915_private_t *dev_priv = dev->dev_private;
  500. ssize_t remain;
  501. loff_t offset, page_base;
  502. char __user *user_data;
  503. int page_offset, page_length, ret;
  504. ret = i915_gem_object_pin(obj, 0, true, true);
  505. if (ret)
  506. goto out;
  507. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  508. if (ret)
  509. goto out_unpin;
  510. ret = i915_gem_object_put_fence(obj);
  511. if (ret)
  512. goto out_unpin;
  513. user_data = (char __user *) (uintptr_t) args->data_ptr;
  514. remain = args->size;
  515. offset = obj->gtt_offset + args->offset;
  516. while (remain > 0) {
  517. /* Operation in this page
  518. *
  519. * page_base = page offset within aperture
  520. * page_offset = offset within page
  521. * page_length = bytes to copy for this page
  522. */
  523. page_base = offset & PAGE_MASK;
  524. page_offset = offset_in_page(offset);
  525. page_length = remain;
  526. if ((page_offset + remain) > PAGE_SIZE)
  527. page_length = PAGE_SIZE - page_offset;
  528. /* If we get a fault while copying data, then (presumably) our
  529. * source page isn't available. Return the error and we'll
  530. * retry in the slow path.
  531. */
  532. if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
  533. page_offset, user_data, page_length)) {
  534. ret = -EFAULT;
  535. goto out_unpin;
  536. }
  537. remain -= page_length;
  538. user_data += page_length;
  539. offset += page_length;
  540. }
  541. out_unpin:
  542. i915_gem_object_unpin(obj);
  543. out:
  544. return ret;
  545. }
  546. /* Per-page copy function for the shmem pwrite fastpath.
  547. * Flushes invalid cachelines before writing to the target if
  548. * needs_clflush_before is set and flushes out any written cachelines after
  549. * writing if needs_clflush is set. */
  550. static int
  551. shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
  552. char __user *user_data,
  553. bool page_do_bit17_swizzling,
  554. bool needs_clflush_before,
  555. bool needs_clflush_after)
  556. {
  557. char *vaddr;
  558. int ret;
  559. if (unlikely(page_do_bit17_swizzling))
  560. return -EINVAL;
  561. vaddr = kmap_atomic(page);
  562. if (needs_clflush_before)
  563. drm_clflush_virt_range(vaddr + shmem_page_offset,
  564. page_length);
  565. ret = __copy_from_user_inatomic_nocache(vaddr + shmem_page_offset,
  566. user_data,
  567. page_length);
  568. if (needs_clflush_after)
  569. drm_clflush_virt_range(vaddr + shmem_page_offset,
  570. page_length);
  571. kunmap_atomic(vaddr);
  572. return ret ? -EFAULT : 0;
  573. }
  574. /* Only difference to the fast-path function is that this can handle bit17
  575. * and uses non-atomic copy and kmap functions. */
  576. static int
  577. shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
  578. char __user *user_data,
  579. bool page_do_bit17_swizzling,
  580. bool needs_clflush_before,
  581. bool needs_clflush_after)
  582. {
  583. char *vaddr;
  584. int ret;
  585. vaddr = kmap(page);
  586. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  587. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  588. page_length,
  589. page_do_bit17_swizzling);
  590. if (page_do_bit17_swizzling)
  591. ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
  592. user_data,
  593. page_length);
  594. else
  595. ret = __copy_from_user(vaddr + shmem_page_offset,
  596. user_data,
  597. page_length);
  598. if (needs_clflush_after)
  599. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  600. page_length,
  601. page_do_bit17_swizzling);
  602. kunmap(page);
  603. return ret ? -EFAULT : 0;
  604. }
  605. static int
  606. i915_gem_shmem_pwrite(struct drm_device *dev,
  607. struct drm_i915_gem_object *obj,
  608. struct drm_i915_gem_pwrite *args,
  609. struct drm_file *file)
  610. {
  611. ssize_t remain;
  612. loff_t offset;
  613. char __user *user_data;
  614. int shmem_page_offset, page_length, ret = 0;
  615. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  616. int hit_slowpath = 0;
  617. int needs_clflush_after = 0;
  618. int needs_clflush_before = 0;
  619. int i;
  620. struct scatterlist *sg;
  621. user_data = (char __user *) (uintptr_t) args->data_ptr;
  622. remain = args->size;
  623. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  624. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  625. /* If we're not in the cpu write domain, set ourself into the gtt
  626. * write domain and manually flush cachelines (if required). This
  627. * optimizes for the case when the gpu will use the data
  628. * right away and we therefore have to clflush anyway. */
  629. if (obj->cache_level == I915_CACHE_NONE)
  630. needs_clflush_after = 1;
  631. if (obj->gtt_space) {
  632. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  633. if (ret)
  634. return ret;
  635. }
  636. }
  637. /* Same trick applies for invalidate partially written cachelines before
  638. * writing. */
  639. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)
  640. && obj->cache_level == I915_CACHE_NONE)
  641. needs_clflush_before = 1;
  642. ret = i915_gem_object_get_pages(obj);
  643. if (ret)
  644. return ret;
  645. i915_gem_object_pin_pages(obj);
  646. offset = args->offset;
  647. obj->dirty = 1;
  648. for_each_sg(obj->pages->sgl, sg, obj->pages->nents, i) {
  649. struct page *page;
  650. int partial_cacheline_write;
  651. if (i < offset >> PAGE_SHIFT)
  652. continue;
  653. if (remain <= 0)
  654. break;
  655. /* Operation in this page
  656. *
  657. * shmem_page_offset = offset within page in shmem file
  658. * page_length = bytes to copy for this page
  659. */
  660. shmem_page_offset = offset_in_page(offset);
  661. page_length = remain;
  662. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  663. page_length = PAGE_SIZE - shmem_page_offset;
  664. /* If we don't overwrite a cacheline completely we need to be
  665. * careful to have up-to-date data by first clflushing. Don't
  666. * overcomplicate things and flush the entire patch. */
  667. partial_cacheline_write = needs_clflush_before &&
  668. ((shmem_page_offset | page_length)
  669. & (boot_cpu_data.x86_clflush_size - 1));
  670. page = sg_page(sg);
  671. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  672. (page_to_phys(page) & (1 << 17)) != 0;
  673. ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
  674. user_data, page_do_bit17_swizzling,
  675. partial_cacheline_write,
  676. needs_clflush_after);
  677. if (ret == 0)
  678. goto next_page;
  679. hit_slowpath = 1;
  680. mutex_unlock(&dev->struct_mutex);
  681. ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
  682. user_data, page_do_bit17_swizzling,
  683. partial_cacheline_write,
  684. needs_clflush_after);
  685. mutex_lock(&dev->struct_mutex);
  686. next_page:
  687. set_page_dirty(page);
  688. mark_page_accessed(page);
  689. if (ret)
  690. goto out;
  691. remain -= page_length;
  692. user_data += page_length;
  693. offset += page_length;
  694. }
  695. out:
  696. i915_gem_object_unpin_pages(obj);
  697. if (hit_slowpath) {
  698. /* Fixup: Flush dirty cachelines in case the object isn't in the
  699. * cpu write domain anymore. */
  700. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  701. i915_gem_clflush_object(obj);
  702. i915_gem_chipset_flush(dev);
  703. }
  704. }
  705. if (needs_clflush_after)
  706. i915_gem_chipset_flush(dev);
  707. return ret;
  708. }
  709. /**
  710. * Writes data to the object referenced by handle.
  711. *
  712. * On error, the contents of the buffer that were to be modified are undefined.
  713. */
  714. int
  715. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  716. struct drm_file *file)
  717. {
  718. struct drm_i915_gem_pwrite *args = data;
  719. struct drm_i915_gem_object *obj;
  720. int ret;
  721. if (args->size == 0)
  722. return 0;
  723. if (!access_ok(VERIFY_READ,
  724. (char __user *)(uintptr_t)args->data_ptr,
  725. args->size))
  726. return -EFAULT;
  727. ret = fault_in_multipages_readable((char __user *)(uintptr_t)args->data_ptr,
  728. args->size);
  729. if (ret)
  730. return -EFAULT;
  731. ret = i915_mutex_lock_interruptible(dev);
  732. if (ret)
  733. return ret;
  734. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  735. if (&obj->base == NULL) {
  736. ret = -ENOENT;
  737. goto unlock;
  738. }
  739. /* Bounds check destination. */
  740. if (args->offset > obj->base.size ||
  741. args->size > obj->base.size - args->offset) {
  742. ret = -EINVAL;
  743. goto out;
  744. }
  745. /* prime objects have no backing filp to GEM pread/pwrite
  746. * pages from.
  747. */
  748. if (!obj->base.filp) {
  749. ret = -EINVAL;
  750. goto out;
  751. }
  752. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  753. ret = -EFAULT;
  754. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  755. * it would end up going through the fenced access, and we'll get
  756. * different detiling behavior between reading and writing.
  757. * pread/pwrite currently are reading and writing from the CPU
  758. * perspective, requiring manual detiling by the client.
  759. */
  760. if (obj->phys_obj) {
  761. ret = i915_gem_phys_pwrite(dev, obj, args, file);
  762. goto out;
  763. }
  764. if (obj->cache_level == I915_CACHE_NONE &&
  765. obj->tiling_mode == I915_TILING_NONE &&
  766. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  767. ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
  768. /* Note that the gtt paths might fail with non-page-backed user
  769. * pointers (e.g. gtt mappings when moving data between
  770. * textures). Fallback to the shmem path in that case. */
  771. }
  772. if (ret == -EFAULT || ret == -ENOSPC)
  773. ret = i915_gem_shmem_pwrite(dev, obj, args, file);
  774. out:
  775. drm_gem_object_unreference(&obj->base);
  776. unlock:
  777. mutex_unlock(&dev->struct_mutex);
  778. return ret;
  779. }
  780. int
  781. i915_gem_check_wedge(struct drm_i915_private *dev_priv,
  782. bool interruptible)
  783. {
  784. if (atomic_read(&dev_priv->mm.wedged)) {
  785. struct completion *x = &dev_priv->error_completion;
  786. bool recovery_complete;
  787. unsigned long flags;
  788. /* Give the error handler a chance to run. */
  789. spin_lock_irqsave(&x->wait.lock, flags);
  790. recovery_complete = x->done > 0;
  791. spin_unlock_irqrestore(&x->wait.lock, flags);
  792. /* Non-interruptible callers can't handle -EAGAIN, hence return
  793. * -EIO unconditionally for these. */
  794. if (!interruptible)
  795. return -EIO;
  796. /* Recovery complete, but still wedged means reset failure. */
  797. if (recovery_complete)
  798. return -EIO;
  799. return -EAGAIN;
  800. }
  801. return 0;
  802. }
  803. /*
  804. * Compare seqno against outstanding lazy request. Emit a request if they are
  805. * equal.
  806. */
  807. static int
  808. i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno)
  809. {
  810. int ret;
  811. BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex));
  812. ret = 0;
  813. if (seqno == ring->outstanding_lazy_request)
  814. ret = i915_add_request(ring, NULL, NULL);
  815. return ret;
  816. }
  817. /**
  818. * __wait_seqno - wait until execution of seqno has finished
  819. * @ring: the ring expected to report seqno
  820. * @seqno: duh!
  821. * @interruptible: do an interruptible wait (normally yes)
  822. * @timeout: in - how long to wait (NULL forever); out - how much time remaining
  823. *
  824. * Returns 0 if the seqno was found within the alloted time. Else returns the
  825. * errno with remaining time filled in timeout argument.
  826. */
  827. static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno,
  828. bool interruptible, struct timespec *timeout)
  829. {
  830. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  831. struct timespec before, now, wait_time={1,0};
  832. unsigned long timeout_jiffies;
  833. long end;
  834. bool wait_forever = true;
  835. int ret;
  836. if (i915_seqno_passed(ring->get_seqno(ring, true), seqno))
  837. return 0;
  838. trace_i915_gem_request_wait_begin(ring, seqno);
  839. if (timeout != NULL) {
  840. wait_time = *timeout;
  841. wait_forever = false;
  842. }
  843. timeout_jiffies = timespec_to_jiffies(&wait_time);
  844. if (WARN_ON(!ring->irq_get(ring)))
  845. return -ENODEV;
  846. /* Record current time in case interrupted by signal, or wedged * */
  847. getrawmonotonic(&before);
  848. #define EXIT_COND \
  849. (i915_seqno_passed(ring->get_seqno(ring, false), seqno) || \
  850. atomic_read(&dev_priv->mm.wedged))
  851. do {
  852. if (interruptible)
  853. end = wait_event_interruptible_timeout(ring->irq_queue,
  854. EXIT_COND,
  855. timeout_jiffies);
  856. else
  857. end = wait_event_timeout(ring->irq_queue, EXIT_COND,
  858. timeout_jiffies);
  859. ret = i915_gem_check_wedge(dev_priv, interruptible);
  860. if (ret)
  861. end = ret;
  862. } while (end == 0 && wait_forever);
  863. getrawmonotonic(&now);
  864. ring->irq_put(ring);
  865. trace_i915_gem_request_wait_end(ring, seqno);
  866. #undef EXIT_COND
  867. if (timeout) {
  868. struct timespec sleep_time = timespec_sub(now, before);
  869. *timeout = timespec_sub(*timeout, sleep_time);
  870. }
  871. switch (end) {
  872. case -EIO:
  873. case -EAGAIN: /* Wedged */
  874. case -ERESTARTSYS: /* Signal */
  875. return (int)end;
  876. case 0: /* Timeout */
  877. if (timeout)
  878. set_normalized_timespec(timeout, 0, 0);
  879. return -ETIME;
  880. default: /* Completed */
  881. WARN_ON(end < 0); /* We're not aware of other errors */
  882. return 0;
  883. }
  884. }
  885. /**
  886. * Waits for a sequence number to be signaled, and cleans up the
  887. * request and object lists appropriately for that event.
  888. */
  889. int
  890. i915_wait_seqno(struct intel_ring_buffer *ring, uint32_t seqno)
  891. {
  892. struct drm_device *dev = ring->dev;
  893. struct drm_i915_private *dev_priv = dev->dev_private;
  894. bool interruptible = dev_priv->mm.interruptible;
  895. int ret;
  896. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  897. BUG_ON(seqno == 0);
  898. ret = i915_gem_check_wedge(dev_priv, interruptible);
  899. if (ret)
  900. return ret;
  901. ret = i915_gem_check_olr(ring, seqno);
  902. if (ret)
  903. return ret;
  904. return __wait_seqno(ring, seqno, interruptible, NULL);
  905. }
  906. /**
  907. * Ensures that all rendering to the object has completed and the object is
  908. * safe to unbind from the GTT or access from the CPU.
  909. */
  910. static __must_check int
  911. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  912. bool readonly)
  913. {
  914. struct intel_ring_buffer *ring = obj->ring;
  915. u32 seqno;
  916. int ret;
  917. seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
  918. if (seqno == 0)
  919. return 0;
  920. ret = i915_wait_seqno(ring, seqno);
  921. if (ret)
  922. return ret;
  923. i915_gem_retire_requests_ring(ring);
  924. /* Manually manage the write flush as we may have not yet
  925. * retired the buffer.
  926. */
  927. if (obj->last_write_seqno &&
  928. i915_seqno_passed(seqno, obj->last_write_seqno)) {
  929. obj->last_write_seqno = 0;
  930. obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
  931. }
  932. return 0;
  933. }
  934. /* A nonblocking variant of the above wait. This is a highly dangerous routine
  935. * as the object state may change during this call.
  936. */
  937. static __must_check int
  938. i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
  939. bool readonly)
  940. {
  941. struct drm_device *dev = obj->base.dev;
  942. struct drm_i915_private *dev_priv = dev->dev_private;
  943. struct intel_ring_buffer *ring = obj->ring;
  944. u32 seqno;
  945. int ret;
  946. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  947. BUG_ON(!dev_priv->mm.interruptible);
  948. seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
  949. if (seqno == 0)
  950. return 0;
  951. ret = i915_gem_check_wedge(dev_priv, true);
  952. if (ret)
  953. return ret;
  954. ret = i915_gem_check_olr(ring, seqno);
  955. if (ret)
  956. return ret;
  957. mutex_unlock(&dev->struct_mutex);
  958. ret = __wait_seqno(ring, seqno, true, NULL);
  959. mutex_lock(&dev->struct_mutex);
  960. i915_gem_retire_requests_ring(ring);
  961. /* Manually manage the write flush as we may have not yet
  962. * retired the buffer.
  963. */
  964. if (obj->last_write_seqno &&
  965. i915_seqno_passed(seqno, obj->last_write_seqno)) {
  966. obj->last_write_seqno = 0;
  967. obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
  968. }
  969. return ret;
  970. }
  971. /**
  972. * Called when user space prepares to use an object with the CPU, either
  973. * through the mmap ioctl's mapping or a GTT mapping.
  974. */
  975. int
  976. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  977. struct drm_file *file)
  978. {
  979. struct drm_i915_gem_set_domain *args = data;
  980. struct drm_i915_gem_object *obj;
  981. uint32_t read_domains = args->read_domains;
  982. uint32_t write_domain = args->write_domain;
  983. int ret;
  984. /* Only handle setting domains to types used by the CPU. */
  985. if (write_domain & I915_GEM_GPU_DOMAINS)
  986. return -EINVAL;
  987. if (read_domains & I915_GEM_GPU_DOMAINS)
  988. return -EINVAL;
  989. /* Having something in the write domain implies it's in the read
  990. * domain, and only that read domain. Enforce that in the request.
  991. */
  992. if (write_domain != 0 && read_domains != write_domain)
  993. return -EINVAL;
  994. ret = i915_mutex_lock_interruptible(dev);
  995. if (ret)
  996. return ret;
  997. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  998. if (&obj->base == NULL) {
  999. ret = -ENOENT;
  1000. goto unlock;
  1001. }
  1002. /* Try to flush the object off the GPU without holding the lock.
  1003. * We will repeat the flush holding the lock in the normal manner
  1004. * to catch cases where we are gazumped.
  1005. */
  1006. ret = i915_gem_object_wait_rendering__nonblocking(obj, !write_domain);
  1007. if (ret)
  1008. goto unref;
  1009. if (read_domains & I915_GEM_DOMAIN_GTT) {
  1010. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  1011. /* Silently promote "you're not bound, there was nothing to do"
  1012. * to success, since the client was just asking us to
  1013. * make sure everything was done.
  1014. */
  1015. if (ret == -EINVAL)
  1016. ret = 0;
  1017. } else {
  1018. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  1019. }
  1020. unref:
  1021. drm_gem_object_unreference(&obj->base);
  1022. unlock:
  1023. mutex_unlock(&dev->struct_mutex);
  1024. return ret;
  1025. }
  1026. /**
  1027. * Called when user space has done writes to this buffer
  1028. */
  1029. int
  1030. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1031. struct drm_file *file)
  1032. {
  1033. struct drm_i915_gem_sw_finish *args = data;
  1034. struct drm_i915_gem_object *obj;
  1035. int ret = 0;
  1036. ret = i915_mutex_lock_interruptible(dev);
  1037. if (ret)
  1038. return ret;
  1039. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1040. if (&obj->base == NULL) {
  1041. ret = -ENOENT;
  1042. goto unlock;
  1043. }
  1044. /* Pinned buffers may be scanout, so flush the cache */
  1045. if (obj->pin_count)
  1046. i915_gem_object_flush_cpu_write_domain(obj);
  1047. drm_gem_object_unreference(&obj->base);
  1048. unlock:
  1049. mutex_unlock(&dev->struct_mutex);
  1050. return ret;
  1051. }
  1052. /**
  1053. * Maps the contents of an object, returning the address it is mapped
  1054. * into.
  1055. *
  1056. * While the mapping holds a reference on the contents of the object, it doesn't
  1057. * imply a ref on the object itself.
  1058. */
  1059. int
  1060. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1061. struct drm_file *file)
  1062. {
  1063. struct drm_i915_gem_mmap *args = data;
  1064. struct drm_gem_object *obj;
  1065. unsigned long addr;
  1066. obj = drm_gem_object_lookup(dev, file, args->handle);
  1067. if (obj == NULL)
  1068. return -ENOENT;
  1069. /* prime objects have no backing filp to GEM mmap
  1070. * pages from.
  1071. */
  1072. if (!obj->filp) {
  1073. drm_gem_object_unreference_unlocked(obj);
  1074. return -EINVAL;
  1075. }
  1076. addr = vm_mmap(obj->filp, 0, args->size,
  1077. PROT_READ | PROT_WRITE, MAP_SHARED,
  1078. args->offset);
  1079. drm_gem_object_unreference_unlocked(obj);
  1080. if (IS_ERR((void *)addr))
  1081. return addr;
  1082. args->addr_ptr = (uint64_t) addr;
  1083. return 0;
  1084. }
  1085. /**
  1086. * i915_gem_fault - fault a page into the GTT
  1087. * vma: VMA in question
  1088. * vmf: fault info
  1089. *
  1090. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1091. * from userspace. The fault handler takes care of binding the object to
  1092. * the GTT (if needed), allocating and programming a fence register (again,
  1093. * only if needed based on whether the old reg is still valid or the object
  1094. * is tiled) and inserting a new PTE into the faulting process.
  1095. *
  1096. * Note that the faulting process may involve evicting existing objects
  1097. * from the GTT and/or fence registers to make room. So performance may
  1098. * suffer if the GTT working set is large or there are few fence registers
  1099. * left.
  1100. */
  1101. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1102. {
  1103. struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
  1104. struct drm_device *dev = obj->base.dev;
  1105. drm_i915_private_t *dev_priv = dev->dev_private;
  1106. pgoff_t page_offset;
  1107. unsigned long pfn;
  1108. int ret = 0;
  1109. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1110. /* We don't use vmf->pgoff since that has the fake offset */
  1111. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  1112. PAGE_SHIFT;
  1113. ret = i915_mutex_lock_interruptible(dev);
  1114. if (ret)
  1115. goto out;
  1116. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1117. /* Now bind it into the GTT if needed */
  1118. ret = i915_gem_object_pin(obj, 0, true, false);
  1119. if (ret)
  1120. goto unlock;
  1121. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1122. if (ret)
  1123. goto unpin;
  1124. ret = i915_gem_object_get_fence(obj);
  1125. if (ret)
  1126. goto unpin;
  1127. obj->fault_mappable = true;
  1128. pfn = ((dev_priv->mm.gtt_base_addr + obj->gtt_offset) >> PAGE_SHIFT) +
  1129. page_offset;
  1130. /* Finally, remap it using the new GTT offset */
  1131. ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
  1132. unpin:
  1133. i915_gem_object_unpin(obj);
  1134. unlock:
  1135. mutex_unlock(&dev->struct_mutex);
  1136. out:
  1137. switch (ret) {
  1138. case -EIO:
  1139. /* If this -EIO is due to a gpu hang, give the reset code a
  1140. * chance to clean up the mess. Otherwise return the proper
  1141. * SIGBUS. */
  1142. if (!atomic_read(&dev_priv->mm.wedged))
  1143. return VM_FAULT_SIGBUS;
  1144. case -EAGAIN:
  1145. /* Give the error handler a chance to run and move the
  1146. * objects off the GPU active list. Next time we service the
  1147. * fault, we should be able to transition the page into the
  1148. * GTT without touching the GPU (and so avoid further
  1149. * EIO/EGAIN). If the GPU is wedged, then there is no issue
  1150. * with coherency, just lost writes.
  1151. */
  1152. set_need_resched();
  1153. case 0:
  1154. case -ERESTARTSYS:
  1155. case -EINTR:
  1156. case -EBUSY:
  1157. /*
  1158. * EBUSY is ok: this just means that another thread
  1159. * already did the job.
  1160. */
  1161. return VM_FAULT_NOPAGE;
  1162. case -ENOMEM:
  1163. return VM_FAULT_OOM;
  1164. case -ENOSPC:
  1165. return VM_FAULT_SIGBUS;
  1166. default:
  1167. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1168. return VM_FAULT_SIGBUS;
  1169. }
  1170. }
  1171. /**
  1172. * i915_gem_release_mmap - remove physical page mappings
  1173. * @obj: obj in question
  1174. *
  1175. * Preserve the reservation of the mmapping with the DRM core code, but
  1176. * relinquish ownership of the pages back to the system.
  1177. *
  1178. * It is vital that we remove the page mapping if we have mapped a tiled
  1179. * object through the GTT and then lose the fence register due to
  1180. * resource pressure. Similarly if the object has been moved out of the
  1181. * aperture, than pages mapped into userspace must be revoked. Removing the
  1182. * mapping will then trigger a page fault on the next user access, allowing
  1183. * fixup by i915_gem_fault().
  1184. */
  1185. void
  1186. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1187. {
  1188. if (!obj->fault_mappable)
  1189. return;
  1190. if (obj->base.dev->dev_mapping)
  1191. unmap_mapping_range(obj->base.dev->dev_mapping,
  1192. (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
  1193. obj->base.size, 1);
  1194. obj->fault_mappable = false;
  1195. }
  1196. static uint32_t
  1197. i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
  1198. {
  1199. uint32_t gtt_size;
  1200. if (INTEL_INFO(dev)->gen >= 4 ||
  1201. tiling_mode == I915_TILING_NONE)
  1202. return size;
  1203. /* Previous chips need a power-of-two fence region when tiling */
  1204. if (INTEL_INFO(dev)->gen == 3)
  1205. gtt_size = 1024*1024;
  1206. else
  1207. gtt_size = 512*1024;
  1208. while (gtt_size < size)
  1209. gtt_size <<= 1;
  1210. return gtt_size;
  1211. }
  1212. /**
  1213. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1214. * @obj: object to check
  1215. *
  1216. * Return the required GTT alignment for an object, taking into account
  1217. * potential fence register mapping.
  1218. */
  1219. static uint32_t
  1220. i915_gem_get_gtt_alignment(struct drm_device *dev,
  1221. uint32_t size,
  1222. int tiling_mode)
  1223. {
  1224. /*
  1225. * Minimum alignment is 4k (GTT page size), but might be greater
  1226. * if a fence register is needed for the object.
  1227. */
  1228. if (INTEL_INFO(dev)->gen >= 4 ||
  1229. tiling_mode == I915_TILING_NONE)
  1230. return 4096;
  1231. /*
  1232. * Previous chips need to be aligned to the size of the smallest
  1233. * fence register that can contain the object.
  1234. */
  1235. return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1236. }
  1237. /**
  1238. * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
  1239. * unfenced object
  1240. * @dev: the device
  1241. * @size: size of the object
  1242. * @tiling_mode: tiling mode of the object
  1243. *
  1244. * Return the required GTT alignment for an object, only taking into account
  1245. * unfenced tiled surface requirements.
  1246. */
  1247. uint32_t
  1248. i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
  1249. uint32_t size,
  1250. int tiling_mode)
  1251. {
  1252. /*
  1253. * Minimum alignment is 4k (GTT page size) for sane hw.
  1254. */
  1255. if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
  1256. tiling_mode == I915_TILING_NONE)
  1257. return 4096;
  1258. /* Previous hardware however needs to be aligned to a power-of-two
  1259. * tile height. The simplest method for determining this is to reuse
  1260. * the power-of-tile object size.
  1261. */
  1262. return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1263. }
  1264. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1265. {
  1266. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1267. int ret;
  1268. if (obj->base.map_list.map)
  1269. return 0;
  1270. ret = drm_gem_create_mmap_offset(&obj->base);
  1271. if (ret != -ENOSPC)
  1272. return ret;
  1273. /* Badly fragmented mmap space? The only way we can recover
  1274. * space is by destroying unwanted objects. We can't randomly release
  1275. * mmap_offsets as userspace expects them to be persistent for the
  1276. * lifetime of the objects. The closest we can is to release the
  1277. * offsets on purgeable objects by truncating it and marking it purged,
  1278. * which prevents userspace from ever using that object again.
  1279. */
  1280. i915_gem_purge(dev_priv, obj->base.size >> PAGE_SHIFT);
  1281. ret = drm_gem_create_mmap_offset(&obj->base);
  1282. if (ret != -ENOSPC)
  1283. return ret;
  1284. i915_gem_shrink_all(dev_priv);
  1285. return drm_gem_create_mmap_offset(&obj->base);
  1286. }
  1287. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1288. {
  1289. if (!obj->base.map_list.map)
  1290. return;
  1291. drm_gem_free_mmap_offset(&obj->base);
  1292. }
  1293. int
  1294. i915_gem_mmap_gtt(struct drm_file *file,
  1295. struct drm_device *dev,
  1296. uint32_t handle,
  1297. uint64_t *offset)
  1298. {
  1299. struct drm_i915_private *dev_priv = dev->dev_private;
  1300. struct drm_i915_gem_object *obj;
  1301. int ret;
  1302. ret = i915_mutex_lock_interruptible(dev);
  1303. if (ret)
  1304. return ret;
  1305. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  1306. if (&obj->base == NULL) {
  1307. ret = -ENOENT;
  1308. goto unlock;
  1309. }
  1310. if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
  1311. ret = -E2BIG;
  1312. goto out;
  1313. }
  1314. if (obj->madv != I915_MADV_WILLNEED) {
  1315. DRM_ERROR("Attempting to mmap a purgeable buffer\n");
  1316. ret = -EINVAL;
  1317. goto out;
  1318. }
  1319. ret = i915_gem_object_create_mmap_offset(obj);
  1320. if (ret)
  1321. goto out;
  1322. *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
  1323. out:
  1324. drm_gem_object_unreference(&obj->base);
  1325. unlock:
  1326. mutex_unlock(&dev->struct_mutex);
  1327. return ret;
  1328. }
  1329. /**
  1330. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1331. * @dev: DRM device
  1332. * @data: GTT mapping ioctl data
  1333. * @file: GEM object info
  1334. *
  1335. * Simply returns the fake offset to userspace so it can mmap it.
  1336. * The mmap call will end up in drm_gem_mmap(), which will set things
  1337. * up so we can get faults in the handler above.
  1338. *
  1339. * The fault handler will take care of binding the object into the GTT
  1340. * (since it may have been evicted to make room for something), allocating
  1341. * a fence register, and mapping the appropriate aperture address into
  1342. * userspace.
  1343. */
  1344. int
  1345. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1346. struct drm_file *file)
  1347. {
  1348. struct drm_i915_gem_mmap_gtt *args = data;
  1349. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1350. }
  1351. /* Immediately discard the backing storage */
  1352. static void
  1353. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1354. {
  1355. struct inode *inode;
  1356. i915_gem_object_free_mmap_offset(obj);
  1357. if (obj->base.filp == NULL)
  1358. return;
  1359. /* Our goal here is to return as much of the memory as
  1360. * is possible back to the system as we are called from OOM.
  1361. * To do this we must instruct the shmfs to drop all of its
  1362. * backing pages, *now*.
  1363. */
  1364. inode = obj->base.filp->f_path.dentry->d_inode;
  1365. shmem_truncate_range(inode, 0, (loff_t)-1);
  1366. obj->madv = __I915_MADV_PURGED;
  1367. }
  1368. static inline int
  1369. i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
  1370. {
  1371. return obj->madv == I915_MADV_DONTNEED;
  1372. }
  1373. static void
  1374. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  1375. {
  1376. int page_count = obj->base.size / PAGE_SIZE;
  1377. struct scatterlist *sg;
  1378. int ret, i;
  1379. BUG_ON(obj->madv == __I915_MADV_PURGED);
  1380. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  1381. if (ret) {
  1382. /* In the event of a disaster, abandon all caches and
  1383. * hope for the best.
  1384. */
  1385. WARN_ON(ret != -EIO);
  1386. i915_gem_clflush_object(obj);
  1387. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  1388. }
  1389. if (i915_gem_object_needs_bit17_swizzle(obj))
  1390. i915_gem_object_save_bit_17_swizzle(obj);
  1391. if (obj->madv == I915_MADV_DONTNEED)
  1392. obj->dirty = 0;
  1393. for_each_sg(obj->pages->sgl, sg, page_count, i) {
  1394. struct page *page = sg_page(sg);
  1395. if (obj->dirty)
  1396. set_page_dirty(page);
  1397. if (obj->madv == I915_MADV_WILLNEED)
  1398. mark_page_accessed(page);
  1399. page_cache_release(page);
  1400. }
  1401. obj->dirty = 0;
  1402. sg_free_table(obj->pages);
  1403. kfree(obj->pages);
  1404. }
  1405. static int
  1406. i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
  1407. {
  1408. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1409. if (obj->pages == NULL)
  1410. return 0;
  1411. BUG_ON(obj->gtt_space);
  1412. if (obj->pages_pin_count)
  1413. return -EBUSY;
  1414. ops->put_pages(obj);
  1415. obj->pages = NULL;
  1416. list_del(&obj->gtt_list);
  1417. if (i915_gem_object_is_purgeable(obj))
  1418. i915_gem_object_truncate(obj);
  1419. return 0;
  1420. }
  1421. static long
  1422. i915_gem_purge(struct drm_i915_private *dev_priv, long target)
  1423. {
  1424. struct drm_i915_gem_object *obj, *next;
  1425. long count = 0;
  1426. list_for_each_entry_safe(obj, next,
  1427. &dev_priv->mm.unbound_list,
  1428. gtt_list) {
  1429. if (i915_gem_object_is_purgeable(obj) &&
  1430. i915_gem_object_put_pages(obj) == 0) {
  1431. count += obj->base.size >> PAGE_SHIFT;
  1432. if (count >= target)
  1433. return count;
  1434. }
  1435. }
  1436. list_for_each_entry_safe(obj, next,
  1437. &dev_priv->mm.inactive_list,
  1438. mm_list) {
  1439. if (i915_gem_object_is_purgeable(obj) &&
  1440. i915_gem_object_unbind(obj) == 0 &&
  1441. i915_gem_object_put_pages(obj) == 0) {
  1442. count += obj->base.size >> PAGE_SHIFT;
  1443. if (count >= target)
  1444. return count;
  1445. }
  1446. }
  1447. return count;
  1448. }
  1449. static void
  1450. i915_gem_shrink_all(struct drm_i915_private *dev_priv)
  1451. {
  1452. struct drm_i915_gem_object *obj, *next;
  1453. i915_gem_evict_everything(dev_priv->dev);
  1454. list_for_each_entry_safe(obj, next, &dev_priv->mm.unbound_list, gtt_list)
  1455. i915_gem_object_put_pages(obj);
  1456. }
  1457. static int
  1458. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  1459. {
  1460. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1461. int page_count, i;
  1462. struct address_space *mapping;
  1463. struct sg_table *st;
  1464. struct scatterlist *sg;
  1465. struct page *page;
  1466. gfp_t gfp;
  1467. /* Assert that the object is not currently in any GPU domain. As it
  1468. * wasn't in the GTT, there shouldn't be any way it could have been in
  1469. * a GPU cache
  1470. */
  1471. BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  1472. BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  1473. st = kmalloc(sizeof(*st), GFP_KERNEL);
  1474. if (st == NULL)
  1475. return -ENOMEM;
  1476. page_count = obj->base.size / PAGE_SIZE;
  1477. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  1478. sg_free_table(st);
  1479. kfree(st);
  1480. return -ENOMEM;
  1481. }
  1482. /* Get the list of pages out of our struct file. They'll be pinned
  1483. * at this point until we release them.
  1484. *
  1485. * Fail silently without starting the shrinker
  1486. */
  1487. mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  1488. gfp = mapping_gfp_mask(mapping);
  1489. gfp |= __GFP_NORETRY | __GFP_NOWARN;
  1490. gfp &= ~(__GFP_IO | __GFP_WAIT);
  1491. for_each_sg(st->sgl, sg, page_count, i) {
  1492. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1493. if (IS_ERR(page)) {
  1494. i915_gem_purge(dev_priv, page_count);
  1495. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1496. }
  1497. if (IS_ERR(page)) {
  1498. /* We've tried hard to allocate the memory by reaping
  1499. * our own buffer, now let the real VM do its job and
  1500. * go down in flames if truly OOM.
  1501. */
  1502. gfp &= ~(__GFP_NORETRY | __GFP_NOWARN);
  1503. gfp |= __GFP_IO | __GFP_WAIT;
  1504. i915_gem_shrink_all(dev_priv);
  1505. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1506. if (IS_ERR(page))
  1507. goto err_pages;
  1508. gfp |= __GFP_NORETRY | __GFP_NOWARN;
  1509. gfp &= ~(__GFP_IO | __GFP_WAIT);
  1510. }
  1511. sg_set_page(sg, page, PAGE_SIZE, 0);
  1512. }
  1513. obj->pages = st;
  1514. if (i915_gem_object_needs_bit17_swizzle(obj))
  1515. i915_gem_object_do_bit_17_swizzle(obj);
  1516. return 0;
  1517. err_pages:
  1518. for_each_sg(st->sgl, sg, i, page_count)
  1519. page_cache_release(sg_page(sg));
  1520. sg_free_table(st);
  1521. kfree(st);
  1522. return PTR_ERR(page);
  1523. }
  1524. /* Ensure that the associated pages are gathered from the backing storage
  1525. * and pinned into our object. i915_gem_object_get_pages() may be called
  1526. * multiple times before they are released by a single call to
  1527. * i915_gem_object_put_pages() - once the pages are no longer referenced
  1528. * either as a result of memory pressure (reaping pages under the shrinker)
  1529. * or as the object is itself released.
  1530. */
  1531. int
  1532. i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  1533. {
  1534. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1535. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1536. int ret;
  1537. if (obj->pages)
  1538. return 0;
  1539. BUG_ON(obj->pages_pin_count);
  1540. ret = ops->get_pages(obj);
  1541. if (ret)
  1542. return ret;
  1543. list_add_tail(&obj->gtt_list, &dev_priv->mm.unbound_list);
  1544. return 0;
  1545. }
  1546. void
  1547. i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
  1548. struct intel_ring_buffer *ring)
  1549. {
  1550. struct drm_device *dev = obj->base.dev;
  1551. struct drm_i915_private *dev_priv = dev->dev_private;
  1552. u32 seqno = intel_ring_get_seqno(ring);
  1553. BUG_ON(ring == NULL);
  1554. obj->ring = ring;
  1555. /* Add a reference if we're newly entering the active list. */
  1556. if (!obj->active) {
  1557. drm_gem_object_reference(&obj->base);
  1558. obj->active = 1;
  1559. }
  1560. /* Move from whatever list we were on to the tail of execution. */
  1561. list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
  1562. list_move_tail(&obj->ring_list, &ring->active_list);
  1563. obj->last_read_seqno = seqno;
  1564. if (obj->fenced_gpu_access) {
  1565. obj->last_fenced_seqno = seqno;
  1566. /* Bump MRU to take account of the delayed flush */
  1567. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  1568. struct drm_i915_fence_reg *reg;
  1569. reg = &dev_priv->fence_regs[obj->fence_reg];
  1570. list_move_tail(&reg->lru_list,
  1571. &dev_priv->mm.fence_list);
  1572. }
  1573. }
  1574. }
  1575. static void
  1576. i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
  1577. {
  1578. struct drm_device *dev = obj->base.dev;
  1579. struct drm_i915_private *dev_priv = dev->dev_private;
  1580. BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
  1581. BUG_ON(!obj->active);
  1582. if (obj->pin_count) /* are we a framebuffer? */
  1583. intel_mark_fb_idle(obj);
  1584. list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  1585. list_del_init(&obj->ring_list);
  1586. obj->ring = NULL;
  1587. obj->last_read_seqno = 0;
  1588. obj->last_write_seqno = 0;
  1589. obj->base.write_domain = 0;
  1590. obj->last_fenced_seqno = 0;
  1591. obj->fenced_gpu_access = false;
  1592. obj->active = 0;
  1593. drm_gem_object_unreference(&obj->base);
  1594. WARN_ON(i915_verify_lists(dev));
  1595. }
  1596. static int
  1597. i915_gem_handle_seqno_wrap(struct drm_device *dev)
  1598. {
  1599. struct drm_i915_private *dev_priv = dev->dev_private;
  1600. struct intel_ring_buffer *ring;
  1601. int ret, i, j;
  1602. /* The hardware uses various monotonic 32-bit counters, if we
  1603. * detect that they will wraparound we need to idle the GPU
  1604. * and reset those counters.
  1605. */
  1606. ret = 0;
  1607. for_each_ring(ring, dev_priv, i) {
  1608. for (j = 0; j < ARRAY_SIZE(ring->sync_seqno); j++)
  1609. ret |= ring->sync_seqno[j] != 0;
  1610. }
  1611. if (ret == 0)
  1612. return ret;
  1613. ret = i915_gpu_idle(dev);
  1614. if (ret)
  1615. return ret;
  1616. i915_gem_retire_requests(dev);
  1617. for_each_ring(ring, dev_priv, i) {
  1618. for (j = 0; j < ARRAY_SIZE(ring->sync_seqno); j++)
  1619. ring->sync_seqno[j] = 0;
  1620. }
  1621. return 0;
  1622. }
  1623. int
  1624. i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
  1625. {
  1626. struct drm_i915_private *dev_priv = dev->dev_private;
  1627. /* reserve 0 for non-seqno */
  1628. if (dev_priv->next_seqno == 0) {
  1629. int ret = i915_gem_handle_seqno_wrap(dev);
  1630. if (ret)
  1631. return ret;
  1632. dev_priv->next_seqno = 1;
  1633. }
  1634. *seqno = dev_priv->next_seqno++;
  1635. return 0;
  1636. }
  1637. int
  1638. i915_add_request(struct intel_ring_buffer *ring,
  1639. struct drm_file *file,
  1640. u32 *out_seqno)
  1641. {
  1642. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  1643. struct drm_i915_gem_request *request;
  1644. u32 request_ring_position;
  1645. int was_empty;
  1646. int ret;
  1647. /*
  1648. * Emit any outstanding flushes - execbuf can fail to emit the flush
  1649. * after having emitted the batchbuffer command. Hence we need to fix
  1650. * things up similar to emitting the lazy request. The difference here
  1651. * is that the flush _must_ happen before the next request, no matter
  1652. * what.
  1653. */
  1654. ret = intel_ring_flush_all_caches(ring);
  1655. if (ret)
  1656. return ret;
  1657. request = kmalloc(sizeof(*request), GFP_KERNEL);
  1658. if (request == NULL)
  1659. return -ENOMEM;
  1660. /* Record the position of the start of the request so that
  1661. * should we detect the updated seqno part-way through the
  1662. * GPU processing the request, we never over-estimate the
  1663. * position of the head.
  1664. */
  1665. request_ring_position = intel_ring_get_tail(ring);
  1666. ret = ring->add_request(ring);
  1667. if (ret) {
  1668. kfree(request);
  1669. return ret;
  1670. }
  1671. request->seqno = intel_ring_get_seqno(ring);
  1672. request->ring = ring;
  1673. request->tail = request_ring_position;
  1674. request->emitted_jiffies = jiffies;
  1675. was_empty = list_empty(&ring->request_list);
  1676. list_add_tail(&request->list, &ring->request_list);
  1677. request->file_priv = NULL;
  1678. if (file) {
  1679. struct drm_i915_file_private *file_priv = file->driver_priv;
  1680. spin_lock(&file_priv->mm.lock);
  1681. request->file_priv = file_priv;
  1682. list_add_tail(&request->client_list,
  1683. &file_priv->mm.request_list);
  1684. spin_unlock(&file_priv->mm.lock);
  1685. }
  1686. trace_i915_gem_request_add(ring, request->seqno);
  1687. ring->outstanding_lazy_request = 0;
  1688. if (!dev_priv->mm.suspended) {
  1689. if (i915_enable_hangcheck) {
  1690. mod_timer(&dev_priv->hangcheck_timer,
  1691. round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
  1692. }
  1693. if (was_empty) {
  1694. queue_delayed_work(dev_priv->wq,
  1695. &dev_priv->mm.retire_work,
  1696. round_jiffies_up_relative(HZ));
  1697. intel_mark_busy(dev_priv->dev);
  1698. }
  1699. }
  1700. if (out_seqno)
  1701. *out_seqno = request->seqno;
  1702. return 0;
  1703. }
  1704. static inline void
  1705. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  1706. {
  1707. struct drm_i915_file_private *file_priv = request->file_priv;
  1708. if (!file_priv)
  1709. return;
  1710. spin_lock(&file_priv->mm.lock);
  1711. if (request->file_priv) {
  1712. list_del(&request->client_list);
  1713. request->file_priv = NULL;
  1714. }
  1715. spin_unlock(&file_priv->mm.lock);
  1716. }
  1717. static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
  1718. struct intel_ring_buffer *ring)
  1719. {
  1720. while (!list_empty(&ring->request_list)) {
  1721. struct drm_i915_gem_request *request;
  1722. request = list_first_entry(&ring->request_list,
  1723. struct drm_i915_gem_request,
  1724. list);
  1725. list_del(&request->list);
  1726. i915_gem_request_remove_from_client(request);
  1727. kfree(request);
  1728. }
  1729. while (!list_empty(&ring->active_list)) {
  1730. struct drm_i915_gem_object *obj;
  1731. obj = list_first_entry(&ring->active_list,
  1732. struct drm_i915_gem_object,
  1733. ring_list);
  1734. i915_gem_object_move_to_inactive(obj);
  1735. }
  1736. }
  1737. static void i915_gem_reset_fences(struct drm_device *dev)
  1738. {
  1739. struct drm_i915_private *dev_priv = dev->dev_private;
  1740. int i;
  1741. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  1742. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  1743. i915_gem_write_fence(dev, i, NULL);
  1744. if (reg->obj)
  1745. i915_gem_object_fence_lost(reg->obj);
  1746. reg->pin_count = 0;
  1747. reg->obj = NULL;
  1748. INIT_LIST_HEAD(&reg->lru_list);
  1749. }
  1750. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  1751. }
  1752. void i915_gem_reset(struct drm_device *dev)
  1753. {
  1754. struct drm_i915_private *dev_priv = dev->dev_private;
  1755. struct drm_i915_gem_object *obj;
  1756. struct intel_ring_buffer *ring;
  1757. int i;
  1758. for_each_ring(ring, dev_priv, i)
  1759. i915_gem_reset_ring_lists(dev_priv, ring);
  1760. /* Move everything out of the GPU domains to ensure we do any
  1761. * necessary invalidation upon reuse.
  1762. */
  1763. list_for_each_entry(obj,
  1764. &dev_priv->mm.inactive_list,
  1765. mm_list)
  1766. {
  1767. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  1768. }
  1769. /* The fence registers are invalidated so clear them out */
  1770. i915_gem_reset_fences(dev);
  1771. }
  1772. /**
  1773. * This function clears the request list as sequence numbers are passed.
  1774. */
  1775. void
  1776. i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
  1777. {
  1778. uint32_t seqno;
  1779. if (list_empty(&ring->request_list))
  1780. return;
  1781. WARN_ON(i915_verify_lists(ring->dev));
  1782. seqno = ring->get_seqno(ring, true);
  1783. while (!list_empty(&ring->request_list)) {
  1784. struct drm_i915_gem_request *request;
  1785. request = list_first_entry(&ring->request_list,
  1786. struct drm_i915_gem_request,
  1787. list);
  1788. if (!i915_seqno_passed(seqno, request->seqno))
  1789. break;
  1790. trace_i915_gem_request_retire(ring, request->seqno);
  1791. /* We know the GPU must have read the request to have
  1792. * sent us the seqno + interrupt, so use the position
  1793. * of tail of the request to update the last known position
  1794. * of the GPU head.
  1795. */
  1796. ring->last_retired_head = request->tail;
  1797. list_del(&request->list);
  1798. i915_gem_request_remove_from_client(request);
  1799. kfree(request);
  1800. }
  1801. /* Move any buffers on the active list that are no longer referenced
  1802. * by the ringbuffer to the flushing/inactive lists as appropriate.
  1803. */
  1804. while (!list_empty(&ring->active_list)) {
  1805. struct drm_i915_gem_object *obj;
  1806. obj = list_first_entry(&ring->active_list,
  1807. struct drm_i915_gem_object,
  1808. ring_list);
  1809. if (!i915_seqno_passed(seqno, obj->last_read_seqno))
  1810. break;
  1811. i915_gem_object_move_to_inactive(obj);
  1812. }
  1813. if (unlikely(ring->trace_irq_seqno &&
  1814. i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
  1815. ring->irq_put(ring);
  1816. ring->trace_irq_seqno = 0;
  1817. }
  1818. WARN_ON(i915_verify_lists(ring->dev));
  1819. }
  1820. void
  1821. i915_gem_retire_requests(struct drm_device *dev)
  1822. {
  1823. drm_i915_private_t *dev_priv = dev->dev_private;
  1824. struct intel_ring_buffer *ring;
  1825. int i;
  1826. for_each_ring(ring, dev_priv, i)
  1827. i915_gem_retire_requests_ring(ring);
  1828. }
  1829. static void
  1830. i915_gem_retire_work_handler(struct work_struct *work)
  1831. {
  1832. drm_i915_private_t *dev_priv;
  1833. struct drm_device *dev;
  1834. struct intel_ring_buffer *ring;
  1835. bool idle;
  1836. int i;
  1837. dev_priv = container_of(work, drm_i915_private_t,
  1838. mm.retire_work.work);
  1839. dev = dev_priv->dev;
  1840. /* Come back later if the device is busy... */
  1841. if (!mutex_trylock(&dev->struct_mutex)) {
  1842. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  1843. round_jiffies_up_relative(HZ));
  1844. return;
  1845. }
  1846. i915_gem_retire_requests(dev);
  1847. /* Send a periodic flush down the ring so we don't hold onto GEM
  1848. * objects indefinitely.
  1849. */
  1850. idle = true;
  1851. for_each_ring(ring, dev_priv, i) {
  1852. if (ring->gpu_caches_dirty)
  1853. i915_add_request(ring, NULL, NULL);
  1854. idle &= list_empty(&ring->request_list);
  1855. }
  1856. if (!dev_priv->mm.suspended && !idle)
  1857. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  1858. round_jiffies_up_relative(HZ));
  1859. if (idle)
  1860. intel_mark_idle(dev);
  1861. mutex_unlock(&dev->struct_mutex);
  1862. }
  1863. /**
  1864. * Ensures that an object will eventually get non-busy by flushing any required
  1865. * write domains, emitting any outstanding lazy request and retiring and
  1866. * completed requests.
  1867. */
  1868. static int
  1869. i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
  1870. {
  1871. int ret;
  1872. if (obj->active) {
  1873. ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
  1874. if (ret)
  1875. return ret;
  1876. i915_gem_retire_requests_ring(obj->ring);
  1877. }
  1878. return 0;
  1879. }
  1880. /**
  1881. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  1882. * @DRM_IOCTL_ARGS: standard ioctl arguments
  1883. *
  1884. * Returns 0 if successful, else an error is returned with the remaining time in
  1885. * the timeout parameter.
  1886. * -ETIME: object is still busy after timeout
  1887. * -ERESTARTSYS: signal interrupted the wait
  1888. * -ENONENT: object doesn't exist
  1889. * Also possible, but rare:
  1890. * -EAGAIN: GPU wedged
  1891. * -ENOMEM: damn
  1892. * -ENODEV: Internal IRQ fail
  1893. * -E?: The add request failed
  1894. *
  1895. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  1896. * non-zero timeout parameter the wait ioctl will wait for the given number of
  1897. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  1898. * without holding struct_mutex the object may become re-busied before this
  1899. * function completes. A similar but shorter * race condition exists in the busy
  1900. * ioctl
  1901. */
  1902. int
  1903. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  1904. {
  1905. struct drm_i915_gem_wait *args = data;
  1906. struct drm_i915_gem_object *obj;
  1907. struct intel_ring_buffer *ring = NULL;
  1908. struct timespec timeout_stack, *timeout = NULL;
  1909. u32 seqno = 0;
  1910. int ret = 0;
  1911. if (args->timeout_ns >= 0) {
  1912. timeout_stack = ns_to_timespec(args->timeout_ns);
  1913. timeout = &timeout_stack;
  1914. }
  1915. ret = i915_mutex_lock_interruptible(dev);
  1916. if (ret)
  1917. return ret;
  1918. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
  1919. if (&obj->base == NULL) {
  1920. mutex_unlock(&dev->struct_mutex);
  1921. return -ENOENT;
  1922. }
  1923. /* Need to make sure the object gets inactive eventually. */
  1924. ret = i915_gem_object_flush_active(obj);
  1925. if (ret)
  1926. goto out;
  1927. if (obj->active) {
  1928. seqno = obj->last_read_seqno;
  1929. ring = obj->ring;
  1930. }
  1931. if (seqno == 0)
  1932. goto out;
  1933. /* Do this after OLR check to make sure we make forward progress polling
  1934. * on this IOCTL with a 0 timeout (like busy ioctl)
  1935. */
  1936. if (!args->timeout_ns) {
  1937. ret = -ETIME;
  1938. goto out;
  1939. }
  1940. drm_gem_object_unreference(&obj->base);
  1941. mutex_unlock(&dev->struct_mutex);
  1942. ret = __wait_seqno(ring, seqno, true, timeout);
  1943. if (timeout) {
  1944. WARN_ON(!timespec_valid(timeout));
  1945. args->timeout_ns = timespec_to_ns(timeout);
  1946. }
  1947. return ret;
  1948. out:
  1949. drm_gem_object_unreference(&obj->base);
  1950. mutex_unlock(&dev->struct_mutex);
  1951. return ret;
  1952. }
  1953. /**
  1954. * i915_gem_object_sync - sync an object to a ring.
  1955. *
  1956. * @obj: object which may be in use on another ring.
  1957. * @to: ring we wish to use the object on. May be NULL.
  1958. *
  1959. * This code is meant to abstract object synchronization with the GPU.
  1960. * Calling with NULL implies synchronizing the object with the CPU
  1961. * rather than a particular GPU ring.
  1962. *
  1963. * Returns 0 if successful, else propagates up the lower layer error.
  1964. */
  1965. int
  1966. i915_gem_object_sync(struct drm_i915_gem_object *obj,
  1967. struct intel_ring_buffer *to)
  1968. {
  1969. struct intel_ring_buffer *from = obj->ring;
  1970. u32 seqno;
  1971. int ret, idx;
  1972. if (from == NULL || to == from)
  1973. return 0;
  1974. if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
  1975. return i915_gem_object_wait_rendering(obj, false);
  1976. idx = intel_ring_sync_index(from, to);
  1977. seqno = obj->last_read_seqno;
  1978. if (seqno <= from->sync_seqno[idx])
  1979. return 0;
  1980. ret = i915_gem_check_olr(obj->ring, seqno);
  1981. if (ret)
  1982. return ret;
  1983. ret = to->sync_to(to, from, seqno);
  1984. if (!ret)
  1985. /* We use last_read_seqno because sync_to()
  1986. * might have just caused seqno wrap under
  1987. * the radar.
  1988. */
  1989. from->sync_seqno[idx] = obj->last_read_seqno;
  1990. return ret;
  1991. }
  1992. static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
  1993. {
  1994. u32 old_write_domain, old_read_domains;
  1995. /* Act a barrier for all accesses through the GTT */
  1996. mb();
  1997. /* Force a pagefault for domain tracking on next user access */
  1998. i915_gem_release_mmap(obj);
  1999. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2000. return;
  2001. old_read_domains = obj->base.read_domains;
  2002. old_write_domain = obj->base.write_domain;
  2003. obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
  2004. obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
  2005. trace_i915_gem_object_change_domain(obj,
  2006. old_read_domains,
  2007. old_write_domain);
  2008. }
  2009. /**
  2010. * Unbinds an object from the GTT aperture.
  2011. */
  2012. int
  2013. i915_gem_object_unbind(struct drm_i915_gem_object *obj)
  2014. {
  2015. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  2016. int ret = 0;
  2017. if (obj->gtt_space == NULL)
  2018. return 0;
  2019. if (obj->pin_count)
  2020. return -EBUSY;
  2021. BUG_ON(obj->pages == NULL);
  2022. ret = i915_gem_object_finish_gpu(obj);
  2023. if (ret)
  2024. return ret;
  2025. /* Continue on if we fail due to EIO, the GPU is hung so we
  2026. * should be safe and we need to cleanup or else we might
  2027. * cause memory corruption through use-after-free.
  2028. */
  2029. i915_gem_object_finish_gtt(obj);
  2030. /* release the fence reg _after_ flushing */
  2031. ret = i915_gem_object_put_fence(obj);
  2032. if (ret)
  2033. return ret;
  2034. trace_i915_gem_object_unbind(obj);
  2035. if (obj->has_global_gtt_mapping)
  2036. i915_gem_gtt_unbind_object(obj);
  2037. if (obj->has_aliasing_ppgtt_mapping) {
  2038. i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
  2039. obj->has_aliasing_ppgtt_mapping = 0;
  2040. }
  2041. i915_gem_gtt_finish_object(obj);
  2042. list_del(&obj->mm_list);
  2043. list_move_tail(&obj->gtt_list, &dev_priv->mm.unbound_list);
  2044. /* Avoid an unnecessary call to unbind on rebind. */
  2045. obj->map_and_fenceable = true;
  2046. drm_mm_put_block(obj->gtt_space);
  2047. obj->gtt_space = NULL;
  2048. obj->gtt_offset = 0;
  2049. return 0;
  2050. }
  2051. int i915_gpu_idle(struct drm_device *dev)
  2052. {
  2053. drm_i915_private_t *dev_priv = dev->dev_private;
  2054. struct intel_ring_buffer *ring;
  2055. int ret, i;
  2056. /* Flush everything onto the inactive list. */
  2057. for_each_ring(ring, dev_priv, i) {
  2058. ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID);
  2059. if (ret)
  2060. return ret;
  2061. ret = intel_ring_idle(ring);
  2062. if (ret)
  2063. return ret;
  2064. }
  2065. return 0;
  2066. }
  2067. static void sandybridge_write_fence_reg(struct drm_device *dev, int reg,
  2068. struct drm_i915_gem_object *obj)
  2069. {
  2070. drm_i915_private_t *dev_priv = dev->dev_private;
  2071. uint64_t val;
  2072. if (obj) {
  2073. u32 size = obj->gtt_space->size;
  2074. val = (uint64_t)((obj->gtt_offset + size - 4096) &
  2075. 0xfffff000) << 32;
  2076. val |= obj->gtt_offset & 0xfffff000;
  2077. val |= (uint64_t)((obj->stride / 128) - 1) <<
  2078. SANDYBRIDGE_FENCE_PITCH_SHIFT;
  2079. if (obj->tiling_mode == I915_TILING_Y)
  2080. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  2081. val |= I965_FENCE_REG_VALID;
  2082. } else
  2083. val = 0;
  2084. I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + reg * 8, val);
  2085. POSTING_READ(FENCE_REG_SANDYBRIDGE_0 + reg * 8);
  2086. }
  2087. static void i965_write_fence_reg(struct drm_device *dev, int reg,
  2088. struct drm_i915_gem_object *obj)
  2089. {
  2090. drm_i915_private_t *dev_priv = dev->dev_private;
  2091. uint64_t val;
  2092. if (obj) {
  2093. u32 size = obj->gtt_space->size;
  2094. val = (uint64_t)((obj->gtt_offset + size - 4096) &
  2095. 0xfffff000) << 32;
  2096. val |= obj->gtt_offset & 0xfffff000;
  2097. val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
  2098. if (obj->tiling_mode == I915_TILING_Y)
  2099. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  2100. val |= I965_FENCE_REG_VALID;
  2101. } else
  2102. val = 0;
  2103. I915_WRITE64(FENCE_REG_965_0 + reg * 8, val);
  2104. POSTING_READ(FENCE_REG_965_0 + reg * 8);
  2105. }
  2106. static void i915_write_fence_reg(struct drm_device *dev, int reg,
  2107. struct drm_i915_gem_object *obj)
  2108. {
  2109. drm_i915_private_t *dev_priv = dev->dev_private;
  2110. u32 val;
  2111. if (obj) {
  2112. u32 size = obj->gtt_space->size;
  2113. int pitch_val;
  2114. int tile_width;
  2115. WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
  2116. (size & -size) != size ||
  2117. (obj->gtt_offset & (size - 1)),
  2118. "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
  2119. obj->gtt_offset, obj->map_and_fenceable, size);
  2120. if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
  2121. tile_width = 128;
  2122. else
  2123. tile_width = 512;
  2124. /* Note: pitch better be a power of two tile widths */
  2125. pitch_val = obj->stride / tile_width;
  2126. pitch_val = ffs(pitch_val) - 1;
  2127. val = obj->gtt_offset;
  2128. if (obj->tiling_mode == I915_TILING_Y)
  2129. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2130. val |= I915_FENCE_SIZE_BITS(size);
  2131. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2132. val |= I830_FENCE_REG_VALID;
  2133. } else
  2134. val = 0;
  2135. if (reg < 8)
  2136. reg = FENCE_REG_830_0 + reg * 4;
  2137. else
  2138. reg = FENCE_REG_945_8 + (reg - 8) * 4;
  2139. I915_WRITE(reg, val);
  2140. POSTING_READ(reg);
  2141. }
  2142. static void i830_write_fence_reg(struct drm_device *dev, int reg,
  2143. struct drm_i915_gem_object *obj)
  2144. {
  2145. drm_i915_private_t *dev_priv = dev->dev_private;
  2146. uint32_t val;
  2147. if (obj) {
  2148. u32 size = obj->gtt_space->size;
  2149. uint32_t pitch_val;
  2150. WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
  2151. (size & -size) != size ||
  2152. (obj->gtt_offset & (size - 1)),
  2153. "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
  2154. obj->gtt_offset, size);
  2155. pitch_val = obj->stride / 128;
  2156. pitch_val = ffs(pitch_val) - 1;
  2157. val = obj->gtt_offset;
  2158. if (obj->tiling_mode == I915_TILING_Y)
  2159. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2160. val |= I830_FENCE_SIZE_BITS(size);
  2161. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2162. val |= I830_FENCE_REG_VALID;
  2163. } else
  2164. val = 0;
  2165. I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
  2166. POSTING_READ(FENCE_REG_830_0 + reg * 4);
  2167. }
  2168. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  2169. struct drm_i915_gem_object *obj)
  2170. {
  2171. switch (INTEL_INFO(dev)->gen) {
  2172. case 7:
  2173. case 6: sandybridge_write_fence_reg(dev, reg, obj); break;
  2174. case 5:
  2175. case 4: i965_write_fence_reg(dev, reg, obj); break;
  2176. case 3: i915_write_fence_reg(dev, reg, obj); break;
  2177. case 2: i830_write_fence_reg(dev, reg, obj); break;
  2178. default: break;
  2179. }
  2180. }
  2181. static inline int fence_number(struct drm_i915_private *dev_priv,
  2182. struct drm_i915_fence_reg *fence)
  2183. {
  2184. return fence - dev_priv->fence_regs;
  2185. }
  2186. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  2187. struct drm_i915_fence_reg *fence,
  2188. bool enable)
  2189. {
  2190. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2191. int reg = fence_number(dev_priv, fence);
  2192. i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
  2193. if (enable) {
  2194. obj->fence_reg = reg;
  2195. fence->obj = obj;
  2196. list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
  2197. } else {
  2198. obj->fence_reg = I915_FENCE_REG_NONE;
  2199. fence->obj = NULL;
  2200. list_del_init(&fence->lru_list);
  2201. }
  2202. }
  2203. static int
  2204. i915_gem_object_flush_fence(struct drm_i915_gem_object *obj)
  2205. {
  2206. if (obj->last_fenced_seqno) {
  2207. int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
  2208. if (ret)
  2209. return ret;
  2210. obj->last_fenced_seqno = 0;
  2211. }
  2212. /* Ensure that all CPU reads are completed before installing a fence
  2213. * and all writes before removing the fence.
  2214. */
  2215. if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
  2216. mb();
  2217. obj->fenced_gpu_access = false;
  2218. return 0;
  2219. }
  2220. int
  2221. i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
  2222. {
  2223. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2224. int ret;
  2225. ret = i915_gem_object_flush_fence(obj);
  2226. if (ret)
  2227. return ret;
  2228. if (obj->fence_reg == I915_FENCE_REG_NONE)
  2229. return 0;
  2230. i915_gem_object_update_fence(obj,
  2231. &dev_priv->fence_regs[obj->fence_reg],
  2232. false);
  2233. i915_gem_object_fence_lost(obj);
  2234. return 0;
  2235. }
  2236. static struct drm_i915_fence_reg *
  2237. i915_find_fence_reg(struct drm_device *dev)
  2238. {
  2239. struct drm_i915_private *dev_priv = dev->dev_private;
  2240. struct drm_i915_fence_reg *reg, *avail;
  2241. int i;
  2242. /* First try to find a free reg */
  2243. avail = NULL;
  2244. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  2245. reg = &dev_priv->fence_regs[i];
  2246. if (!reg->obj)
  2247. return reg;
  2248. if (!reg->pin_count)
  2249. avail = reg;
  2250. }
  2251. if (avail == NULL)
  2252. return NULL;
  2253. /* None available, try to steal one or wait for a user to finish */
  2254. list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
  2255. if (reg->pin_count)
  2256. continue;
  2257. return reg;
  2258. }
  2259. return NULL;
  2260. }
  2261. /**
  2262. * i915_gem_object_get_fence - set up fencing for an object
  2263. * @obj: object to map through a fence reg
  2264. *
  2265. * When mapping objects through the GTT, userspace wants to be able to write
  2266. * to them without having to worry about swizzling if the object is tiled.
  2267. * This function walks the fence regs looking for a free one for @obj,
  2268. * stealing one if it can't find any.
  2269. *
  2270. * It then sets up the reg based on the object's properties: address, pitch
  2271. * and tiling format.
  2272. *
  2273. * For an untiled surface, this removes any existing fence.
  2274. */
  2275. int
  2276. i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
  2277. {
  2278. struct drm_device *dev = obj->base.dev;
  2279. struct drm_i915_private *dev_priv = dev->dev_private;
  2280. bool enable = obj->tiling_mode != I915_TILING_NONE;
  2281. struct drm_i915_fence_reg *reg;
  2282. int ret;
  2283. /* Have we updated the tiling parameters upon the object and so
  2284. * will need to serialise the write to the associated fence register?
  2285. */
  2286. if (obj->fence_dirty) {
  2287. ret = i915_gem_object_flush_fence(obj);
  2288. if (ret)
  2289. return ret;
  2290. }
  2291. /* Just update our place in the LRU if our fence is getting reused. */
  2292. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  2293. reg = &dev_priv->fence_regs[obj->fence_reg];
  2294. if (!obj->fence_dirty) {
  2295. list_move_tail(&reg->lru_list,
  2296. &dev_priv->mm.fence_list);
  2297. return 0;
  2298. }
  2299. } else if (enable) {
  2300. reg = i915_find_fence_reg(dev);
  2301. if (reg == NULL)
  2302. return -EDEADLK;
  2303. if (reg->obj) {
  2304. struct drm_i915_gem_object *old = reg->obj;
  2305. ret = i915_gem_object_flush_fence(old);
  2306. if (ret)
  2307. return ret;
  2308. i915_gem_object_fence_lost(old);
  2309. }
  2310. } else
  2311. return 0;
  2312. i915_gem_object_update_fence(obj, reg, enable);
  2313. obj->fence_dirty = false;
  2314. return 0;
  2315. }
  2316. static bool i915_gem_valid_gtt_space(struct drm_device *dev,
  2317. struct drm_mm_node *gtt_space,
  2318. unsigned long cache_level)
  2319. {
  2320. struct drm_mm_node *other;
  2321. /* On non-LLC machines we have to be careful when putting differing
  2322. * types of snoopable memory together to avoid the prefetcher
  2323. * crossing memory domains and dieing.
  2324. */
  2325. if (HAS_LLC(dev))
  2326. return true;
  2327. if (gtt_space == NULL)
  2328. return true;
  2329. if (list_empty(&gtt_space->node_list))
  2330. return true;
  2331. other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
  2332. if (other->allocated && !other->hole_follows && other->color != cache_level)
  2333. return false;
  2334. other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
  2335. if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
  2336. return false;
  2337. return true;
  2338. }
  2339. static void i915_gem_verify_gtt(struct drm_device *dev)
  2340. {
  2341. #if WATCH_GTT
  2342. struct drm_i915_private *dev_priv = dev->dev_private;
  2343. struct drm_i915_gem_object *obj;
  2344. int err = 0;
  2345. list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list) {
  2346. if (obj->gtt_space == NULL) {
  2347. printk(KERN_ERR "object found on GTT list with no space reserved\n");
  2348. err++;
  2349. continue;
  2350. }
  2351. if (obj->cache_level != obj->gtt_space->color) {
  2352. printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n",
  2353. obj->gtt_space->start,
  2354. obj->gtt_space->start + obj->gtt_space->size,
  2355. obj->cache_level,
  2356. obj->gtt_space->color);
  2357. err++;
  2358. continue;
  2359. }
  2360. if (!i915_gem_valid_gtt_space(dev,
  2361. obj->gtt_space,
  2362. obj->cache_level)) {
  2363. printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n",
  2364. obj->gtt_space->start,
  2365. obj->gtt_space->start + obj->gtt_space->size,
  2366. obj->cache_level);
  2367. err++;
  2368. continue;
  2369. }
  2370. }
  2371. WARN_ON(err);
  2372. #endif
  2373. }
  2374. /**
  2375. * Finds free space in the GTT aperture and binds the object there.
  2376. */
  2377. static int
  2378. i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
  2379. unsigned alignment,
  2380. bool map_and_fenceable,
  2381. bool nonblocking)
  2382. {
  2383. struct drm_device *dev = obj->base.dev;
  2384. drm_i915_private_t *dev_priv = dev->dev_private;
  2385. struct drm_mm_node *free_space;
  2386. u32 size, fence_size, fence_alignment, unfenced_alignment;
  2387. bool mappable, fenceable;
  2388. int ret;
  2389. if (obj->madv != I915_MADV_WILLNEED) {
  2390. DRM_ERROR("Attempting to bind a purgeable object\n");
  2391. return -EINVAL;
  2392. }
  2393. fence_size = i915_gem_get_gtt_size(dev,
  2394. obj->base.size,
  2395. obj->tiling_mode);
  2396. fence_alignment = i915_gem_get_gtt_alignment(dev,
  2397. obj->base.size,
  2398. obj->tiling_mode);
  2399. unfenced_alignment =
  2400. i915_gem_get_unfenced_gtt_alignment(dev,
  2401. obj->base.size,
  2402. obj->tiling_mode);
  2403. if (alignment == 0)
  2404. alignment = map_and_fenceable ? fence_alignment :
  2405. unfenced_alignment;
  2406. if (map_and_fenceable && alignment & (fence_alignment - 1)) {
  2407. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  2408. return -EINVAL;
  2409. }
  2410. size = map_and_fenceable ? fence_size : obj->base.size;
  2411. /* If the object is bigger than the entire aperture, reject it early
  2412. * before evicting everything in a vain attempt to find space.
  2413. */
  2414. if (obj->base.size >
  2415. (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
  2416. DRM_ERROR("Attempting to bind an object larger than the aperture\n");
  2417. return -E2BIG;
  2418. }
  2419. ret = i915_gem_object_get_pages(obj);
  2420. if (ret)
  2421. return ret;
  2422. i915_gem_object_pin_pages(obj);
  2423. search_free:
  2424. if (map_and_fenceable)
  2425. free_space = drm_mm_search_free_in_range_color(&dev_priv->mm.gtt_space,
  2426. size, alignment, obj->cache_level,
  2427. 0, dev_priv->mm.gtt_mappable_end,
  2428. false);
  2429. else
  2430. free_space = drm_mm_search_free_color(&dev_priv->mm.gtt_space,
  2431. size, alignment, obj->cache_level,
  2432. false);
  2433. if (free_space != NULL) {
  2434. if (map_and_fenceable)
  2435. free_space =
  2436. drm_mm_get_block_range_generic(free_space,
  2437. size, alignment, obj->cache_level,
  2438. 0, dev_priv->mm.gtt_mappable_end,
  2439. false);
  2440. else
  2441. free_space =
  2442. drm_mm_get_block_generic(free_space,
  2443. size, alignment, obj->cache_level,
  2444. false);
  2445. }
  2446. if (free_space == NULL) {
  2447. ret = i915_gem_evict_something(dev, size, alignment,
  2448. obj->cache_level,
  2449. map_and_fenceable,
  2450. nonblocking);
  2451. if (ret) {
  2452. i915_gem_object_unpin_pages(obj);
  2453. return ret;
  2454. }
  2455. goto search_free;
  2456. }
  2457. if (WARN_ON(!i915_gem_valid_gtt_space(dev,
  2458. free_space,
  2459. obj->cache_level))) {
  2460. i915_gem_object_unpin_pages(obj);
  2461. drm_mm_put_block(free_space);
  2462. return -EINVAL;
  2463. }
  2464. ret = i915_gem_gtt_prepare_object(obj);
  2465. if (ret) {
  2466. i915_gem_object_unpin_pages(obj);
  2467. drm_mm_put_block(free_space);
  2468. return ret;
  2469. }
  2470. list_move_tail(&obj->gtt_list, &dev_priv->mm.bound_list);
  2471. list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  2472. obj->gtt_space = free_space;
  2473. obj->gtt_offset = free_space->start;
  2474. fenceable =
  2475. free_space->size == fence_size &&
  2476. (free_space->start & (fence_alignment - 1)) == 0;
  2477. mappable =
  2478. obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
  2479. obj->map_and_fenceable = mappable && fenceable;
  2480. i915_gem_object_unpin_pages(obj);
  2481. trace_i915_gem_object_bind(obj, map_and_fenceable);
  2482. i915_gem_verify_gtt(dev);
  2483. return 0;
  2484. }
  2485. void
  2486. i915_gem_clflush_object(struct drm_i915_gem_object *obj)
  2487. {
  2488. /* If we don't have a page list set up, then we're not pinned
  2489. * to GPU, and we can ignore the cache flush because it'll happen
  2490. * again at bind time.
  2491. */
  2492. if (obj->pages == NULL)
  2493. return;
  2494. /* If the GPU is snooping the contents of the CPU cache,
  2495. * we do not need to manually clear the CPU cache lines. However,
  2496. * the caches are only snooped when the render cache is
  2497. * flushed/invalidated. As we always have to emit invalidations
  2498. * and flushes when moving into and out of the RENDER domain, correct
  2499. * snooping behaviour occurs naturally as the result of our domain
  2500. * tracking.
  2501. */
  2502. if (obj->cache_level != I915_CACHE_NONE)
  2503. return;
  2504. trace_i915_gem_object_clflush(obj);
  2505. drm_clflush_sg(obj->pages);
  2506. }
  2507. /** Flushes the GTT write domain for the object if it's dirty. */
  2508. static void
  2509. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  2510. {
  2511. uint32_t old_write_domain;
  2512. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  2513. return;
  2514. /* No actual flushing is required for the GTT write domain. Writes
  2515. * to it immediately go to main memory as far as we know, so there's
  2516. * no chipset flush. It also doesn't land in render cache.
  2517. *
  2518. * However, we do have to enforce the order so that all writes through
  2519. * the GTT land before any writes to the device, such as updates to
  2520. * the GATT itself.
  2521. */
  2522. wmb();
  2523. old_write_domain = obj->base.write_domain;
  2524. obj->base.write_domain = 0;
  2525. trace_i915_gem_object_change_domain(obj,
  2526. obj->base.read_domains,
  2527. old_write_domain);
  2528. }
  2529. /** Flushes the CPU write domain for the object if it's dirty. */
  2530. static void
  2531. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  2532. {
  2533. uint32_t old_write_domain;
  2534. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  2535. return;
  2536. i915_gem_clflush_object(obj);
  2537. i915_gem_chipset_flush(obj->base.dev);
  2538. old_write_domain = obj->base.write_domain;
  2539. obj->base.write_domain = 0;
  2540. trace_i915_gem_object_change_domain(obj,
  2541. obj->base.read_domains,
  2542. old_write_domain);
  2543. }
  2544. /**
  2545. * Moves a single object to the GTT read, and possibly write domain.
  2546. *
  2547. * This function returns when the move is complete, including waiting on
  2548. * flushes to occur.
  2549. */
  2550. int
  2551. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  2552. {
  2553. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  2554. uint32_t old_write_domain, old_read_domains;
  2555. int ret;
  2556. /* Not valid to be called on unbound objects. */
  2557. if (obj->gtt_space == NULL)
  2558. return -EINVAL;
  2559. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  2560. return 0;
  2561. ret = i915_gem_object_wait_rendering(obj, !write);
  2562. if (ret)
  2563. return ret;
  2564. i915_gem_object_flush_cpu_write_domain(obj);
  2565. old_write_domain = obj->base.write_domain;
  2566. old_read_domains = obj->base.read_domains;
  2567. /* It should now be out of any other write domains, and we can update
  2568. * the domain values for our changes.
  2569. */
  2570. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2571. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2572. if (write) {
  2573. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  2574. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  2575. obj->dirty = 1;
  2576. }
  2577. trace_i915_gem_object_change_domain(obj,
  2578. old_read_domains,
  2579. old_write_domain);
  2580. /* And bump the LRU for this access */
  2581. if (i915_gem_object_is_inactive(obj))
  2582. list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  2583. return 0;
  2584. }
  2585. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  2586. enum i915_cache_level cache_level)
  2587. {
  2588. struct drm_device *dev = obj->base.dev;
  2589. drm_i915_private_t *dev_priv = dev->dev_private;
  2590. int ret;
  2591. if (obj->cache_level == cache_level)
  2592. return 0;
  2593. if (obj->pin_count) {
  2594. DRM_DEBUG("can not change the cache level of pinned objects\n");
  2595. return -EBUSY;
  2596. }
  2597. if (!i915_gem_valid_gtt_space(dev, obj->gtt_space, cache_level)) {
  2598. ret = i915_gem_object_unbind(obj);
  2599. if (ret)
  2600. return ret;
  2601. }
  2602. if (obj->gtt_space) {
  2603. ret = i915_gem_object_finish_gpu(obj);
  2604. if (ret)
  2605. return ret;
  2606. i915_gem_object_finish_gtt(obj);
  2607. /* Before SandyBridge, you could not use tiling or fence
  2608. * registers with snooped memory, so relinquish any fences
  2609. * currently pointing to our region in the aperture.
  2610. */
  2611. if (INTEL_INFO(dev)->gen < 6) {
  2612. ret = i915_gem_object_put_fence(obj);
  2613. if (ret)
  2614. return ret;
  2615. }
  2616. if (obj->has_global_gtt_mapping)
  2617. i915_gem_gtt_bind_object(obj, cache_level);
  2618. if (obj->has_aliasing_ppgtt_mapping)
  2619. i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
  2620. obj, cache_level);
  2621. obj->gtt_space->color = cache_level;
  2622. }
  2623. if (cache_level == I915_CACHE_NONE) {
  2624. u32 old_read_domains, old_write_domain;
  2625. /* If we're coming from LLC cached, then we haven't
  2626. * actually been tracking whether the data is in the
  2627. * CPU cache or not, since we only allow one bit set
  2628. * in obj->write_domain and have been skipping the clflushes.
  2629. * Just set it to the CPU cache for now.
  2630. */
  2631. WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
  2632. WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
  2633. old_read_domains = obj->base.read_domains;
  2634. old_write_domain = obj->base.write_domain;
  2635. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2636. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2637. trace_i915_gem_object_change_domain(obj,
  2638. old_read_domains,
  2639. old_write_domain);
  2640. }
  2641. obj->cache_level = cache_level;
  2642. i915_gem_verify_gtt(dev);
  2643. return 0;
  2644. }
  2645. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  2646. struct drm_file *file)
  2647. {
  2648. struct drm_i915_gem_caching *args = data;
  2649. struct drm_i915_gem_object *obj;
  2650. int ret;
  2651. ret = i915_mutex_lock_interruptible(dev);
  2652. if (ret)
  2653. return ret;
  2654. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2655. if (&obj->base == NULL) {
  2656. ret = -ENOENT;
  2657. goto unlock;
  2658. }
  2659. args->caching = obj->cache_level != I915_CACHE_NONE;
  2660. drm_gem_object_unreference(&obj->base);
  2661. unlock:
  2662. mutex_unlock(&dev->struct_mutex);
  2663. return ret;
  2664. }
  2665. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  2666. struct drm_file *file)
  2667. {
  2668. struct drm_i915_gem_caching *args = data;
  2669. struct drm_i915_gem_object *obj;
  2670. enum i915_cache_level level;
  2671. int ret;
  2672. switch (args->caching) {
  2673. case I915_CACHING_NONE:
  2674. level = I915_CACHE_NONE;
  2675. break;
  2676. case I915_CACHING_CACHED:
  2677. level = I915_CACHE_LLC;
  2678. break;
  2679. default:
  2680. return -EINVAL;
  2681. }
  2682. ret = i915_mutex_lock_interruptible(dev);
  2683. if (ret)
  2684. return ret;
  2685. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2686. if (&obj->base == NULL) {
  2687. ret = -ENOENT;
  2688. goto unlock;
  2689. }
  2690. ret = i915_gem_object_set_cache_level(obj, level);
  2691. drm_gem_object_unreference(&obj->base);
  2692. unlock:
  2693. mutex_unlock(&dev->struct_mutex);
  2694. return ret;
  2695. }
  2696. /*
  2697. * Prepare buffer for display plane (scanout, cursors, etc).
  2698. * Can be called from an uninterruptible phase (modesetting) and allows
  2699. * any flushes to be pipelined (for pageflips).
  2700. */
  2701. int
  2702. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  2703. u32 alignment,
  2704. struct intel_ring_buffer *pipelined)
  2705. {
  2706. u32 old_read_domains, old_write_domain;
  2707. int ret;
  2708. if (pipelined != obj->ring) {
  2709. ret = i915_gem_object_sync(obj, pipelined);
  2710. if (ret)
  2711. return ret;
  2712. }
  2713. /* The display engine is not coherent with the LLC cache on gen6. As
  2714. * a result, we make sure that the pinning that is about to occur is
  2715. * done with uncached PTEs. This is lowest common denominator for all
  2716. * chipsets.
  2717. *
  2718. * However for gen6+, we could do better by using the GFDT bit instead
  2719. * of uncaching, which would allow us to flush all the LLC-cached data
  2720. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  2721. */
  2722. ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
  2723. if (ret)
  2724. return ret;
  2725. /* As the user may map the buffer once pinned in the display plane
  2726. * (e.g. libkms for the bootup splash), we have to ensure that we
  2727. * always use map_and_fenceable for all scanout buffers.
  2728. */
  2729. ret = i915_gem_object_pin(obj, alignment, true, false);
  2730. if (ret)
  2731. return ret;
  2732. i915_gem_object_flush_cpu_write_domain(obj);
  2733. old_write_domain = obj->base.write_domain;
  2734. old_read_domains = obj->base.read_domains;
  2735. /* It should now be out of any other write domains, and we can update
  2736. * the domain values for our changes.
  2737. */
  2738. obj->base.write_domain = 0;
  2739. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2740. trace_i915_gem_object_change_domain(obj,
  2741. old_read_domains,
  2742. old_write_domain);
  2743. return 0;
  2744. }
  2745. int
  2746. i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
  2747. {
  2748. int ret;
  2749. if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
  2750. return 0;
  2751. ret = i915_gem_object_wait_rendering(obj, false);
  2752. if (ret)
  2753. return ret;
  2754. /* Ensure that we invalidate the GPU's caches and TLBs. */
  2755. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  2756. return 0;
  2757. }
  2758. /**
  2759. * Moves a single object to the CPU read, and possibly write domain.
  2760. *
  2761. * This function returns when the move is complete, including waiting on
  2762. * flushes to occur.
  2763. */
  2764. int
  2765. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  2766. {
  2767. uint32_t old_write_domain, old_read_domains;
  2768. int ret;
  2769. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  2770. return 0;
  2771. ret = i915_gem_object_wait_rendering(obj, !write);
  2772. if (ret)
  2773. return ret;
  2774. i915_gem_object_flush_gtt_write_domain(obj);
  2775. old_write_domain = obj->base.write_domain;
  2776. old_read_domains = obj->base.read_domains;
  2777. /* Flush the CPU cache if it's still invalid. */
  2778. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  2779. i915_gem_clflush_object(obj);
  2780. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  2781. }
  2782. /* It should now be out of any other write domains, and we can update
  2783. * the domain values for our changes.
  2784. */
  2785. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2786. /* If we're writing through the CPU, then the GPU read domains will
  2787. * need to be invalidated at next use.
  2788. */
  2789. if (write) {
  2790. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2791. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2792. }
  2793. trace_i915_gem_object_change_domain(obj,
  2794. old_read_domains,
  2795. old_write_domain);
  2796. return 0;
  2797. }
  2798. /* Throttle our rendering by waiting until the ring has completed our requests
  2799. * emitted over 20 msec ago.
  2800. *
  2801. * Note that if we were to use the current jiffies each time around the loop,
  2802. * we wouldn't escape the function with any frames outstanding if the time to
  2803. * render a frame was over 20ms.
  2804. *
  2805. * This should get us reasonable parallelism between CPU and GPU but also
  2806. * relatively low latency when blocking on a particular request to finish.
  2807. */
  2808. static int
  2809. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  2810. {
  2811. struct drm_i915_private *dev_priv = dev->dev_private;
  2812. struct drm_i915_file_private *file_priv = file->driver_priv;
  2813. unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
  2814. struct drm_i915_gem_request *request;
  2815. struct intel_ring_buffer *ring = NULL;
  2816. u32 seqno = 0;
  2817. int ret;
  2818. if (atomic_read(&dev_priv->mm.wedged))
  2819. return -EIO;
  2820. spin_lock(&file_priv->mm.lock);
  2821. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  2822. if (time_after_eq(request->emitted_jiffies, recent_enough))
  2823. break;
  2824. ring = request->ring;
  2825. seqno = request->seqno;
  2826. }
  2827. spin_unlock(&file_priv->mm.lock);
  2828. if (seqno == 0)
  2829. return 0;
  2830. ret = __wait_seqno(ring, seqno, true, NULL);
  2831. if (ret == 0)
  2832. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  2833. return ret;
  2834. }
  2835. int
  2836. i915_gem_object_pin(struct drm_i915_gem_object *obj,
  2837. uint32_t alignment,
  2838. bool map_and_fenceable,
  2839. bool nonblocking)
  2840. {
  2841. int ret;
  2842. if (WARN_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
  2843. return -EBUSY;
  2844. if (obj->gtt_space != NULL) {
  2845. if ((alignment && obj->gtt_offset & (alignment - 1)) ||
  2846. (map_and_fenceable && !obj->map_and_fenceable)) {
  2847. WARN(obj->pin_count,
  2848. "bo is already pinned with incorrect alignment:"
  2849. " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
  2850. " obj->map_and_fenceable=%d\n",
  2851. obj->gtt_offset, alignment,
  2852. map_and_fenceable,
  2853. obj->map_and_fenceable);
  2854. ret = i915_gem_object_unbind(obj);
  2855. if (ret)
  2856. return ret;
  2857. }
  2858. }
  2859. if (obj->gtt_space == NULL) {
  2860. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2861. ret = i915_gem_object_bind_to_gtt(obj, alignment,
  2862. map_and_fenceable,
  2863. nonblocking);
  2864. if (ret)
  2865. return ret;
  2866. if (!dev_priv->mm.aliasing_ppgtt)
  2867. i915_gem_gtt_bind_object(obj, obj->cache_level);
  2868. }
  2869. if (!obj->has_global_gtt_mapping && map_and_fenceable)
  2870. i915_gem_gtt_bind_object(obj, obj->cache_level);
  2871. obj->pin_count++;
  2872. obj->pin_mappable |= map_and_fenceable;
  2873. return 0;
  2874. }
  2875. void
  2876. i915_gem_object_unpin(struct drm_i915_gem_object *obj)
  2877. {
  2878. BUG_ON(obj->pin_count == 0);
  2879. BUG_ON(obj->gtt_space == NULL);
  2880. if (--obj->pin_count == 0)
  2881. obj->pin_mappable = false;
  2882. }
  2883. int
  2884. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  2885. struct drm_file *file)
  2886. {
  2887. struct drm_i915_gem_pin *args = data;
  2888. struct drm_i915_gem_object *obj;
  2889. int ret;
  2890. ret = i915_mutex_lock_interruptible(dev);
  2891. if (ret)
  2892. return ret;
  2893. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2894. if (&obj->base == NULL) {
  2895. ret = -ENOENT;
  2896. goto unlock;
  2897. }
  2898. if (obj->madv != I915_MADV_WILLNEED) {
  2899. DRM_ERROR("Attempting to pin a purgeable buffer\n");
  2900. ret = -EINVAL;
  2901. goto out;
  2902. }
  2903. if (obj->pin_filp != NULL && obj->pin_filp != file) {
  2904. DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
  2905. args->handle);
  2906. ret = -EINVAL;
  2907. goto out;
  2908. }
  2909. obj->user_pin_count++;
  2910. obj->pin_filp = file;
  2911. if (obj->user_pin_count == 1) {
  2912. ret = i915_gem_object_pin(obj, args->alignment, true, false);
  2913. if (ret)
  2914. goto out;
  2915. }
  2916. /* XXX - flush the CPU caches for pinned objects
  2917. * as the X server doesn't manage domains yet
  2918. */
  2919. i915_gem_object_flush_cpu_write_domain(obj);
  2920. args->offset = obj->gtt_offset;
  2921. out:
  2922. drm_gem_object_unreference(&obj->base);
  2923. unlock:
  2924. mutex_unlock(&dev->struct_mutex);
  2925. return ret;
  2926. }
  2927. int
  2928. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  2929. struct drm_file *file)
  2930. {
  2931. struct drm_i915_gem_pin *args = data;
  2932. struct drm_i915_gem_object *obj;
  2933. int ret;
  2934. ret = i915_mutex_lock_interruptible(dev);
  2935. if (ret)
  2936. return ret;
  2937. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2938. if (&obj->base == NULL) {
  2939. ret = -ENOENT;
  2940. goto unlock;
  2941. }
  2942. if (obj->pin_filp != file) {
  2943. DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  2944. args->handle);
  2945. ret = -EINVAL;
  2946. goto out;
  2947. }
  2948. obj->user_pin_count--;
  2949. if (obj->user_pin_count == 0) {
  2950. obj->pin_filp = NULL;
  2951. i915_gem_object_unpin(obj);
  2952. }
  2953. out:
  2954. drm_gem_object_unreference(&obj->base);
  2955. unlock:
  2956. mutex_unlock(&dev->struct_mutex);
  2957. return ret;
  2958. }
  2959. int
  2960. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  2961. struct drm_file *file)
  2962. {
  2963. struct drm_i915_gem_busy *args = data;
  2964. struct drm_i915_gem_object *obj;
  2965. int ret;
  2966. ret = i915_mutex_lock_interruptible(dev);
  2967. if (ret)
  2968. return ret;
  2969. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2970. if (&obj->base == NULL) {
  2971. ret = -ENOENT;
  2972. goto unlock;
  2973. }
  2974. /* Count all active objects as busy, even if they are currently not used
  2975. * by the gpu. Users of this interface expect objects to eventually
  2976. * become non-busy without any further actions, therefore emit any
  2977. * necessary flushes here.
  2978. */
  2979. ret = i915_gem_object_flush_active(obj);
  2980. args->busy = obj->active;
  2981. if (obj->ring) {
  2982. BUILD_BUG_ON(I915_NUM_RINGS > 16);
  2983. args->busy |= intel_ring_flag(obj->ring) << 16;
  2984. }
  2985. drm_gem_object_unreference(&obj->base);
  2986. unlock:
  2987. mutex_unlock(&dev->struct_mutex);
  2988. return ret;
  2989. }
  2990. int
  2991. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  2992. struct drm_file *file_priv)
  2993. {
  2994. return i915_gem_ring_throttle(dev, file_priv);
  2995. }
  2996. int
  2997. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  2998. struct drm_file *file_priv)
  2999. {
  3000. struct drm_i915_gem_madvise *args = data;
  3001. struct drm_i915_gem_object *obj;
  3002. int ret;
  3003. switch (args->madv) {
  3004. case I915_MADV_DONTNEED:
  3005. case I915_MADV_WILLNEED:
  3006. break;
  3007. default:
  3008. return -EINVAL;
  3009. }
  3010. ret = i915_mutex_lock_interruptible(dev);
  3011. if (ret)
  3012. return ret;
  3013. obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
  3014. if (&obj->base == NULL) {
  3015. ret = -ENOENT;
  3016. goto unlock;
  3017. }
  3018. if (obj->pin_count) {
  3019. ret = -EINVAL;
  3020. goto out;
  3021. }
  3022. if (obj->madv != __I915_MADV_PURGED)
  3023. obj->madv = args->madv;
  3024. /* if the object is no longer attached, discard its backing storage */
  3025. if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
  3026. i915_gem_object_truncate(obj);
  3027. args->retained = obj->madv != __I915_MADV_PURGED;
  3028. out:
  3029. drm_gem_object_unreference(&obj->base);
  3030. unlock:
  3031. mutex_unlock(&dev->struct_mutex);
  3032. return ret;
  3033. }
  3034. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3035. const struct drm_i915_gem_object_ops *ops)
  3036. {
  3037. INIT_LIST_HEAD(&obj->mm_list);
  3038. INIT_LIST_HEAD(&obj->gtt_list);
  3039. INIT_LIST_HEAD(&obj->ring_list);
  3040. INIT_LIST_HEAD(&obj->exec_list);
  3041. obj->ops = ops;
  3042. obj->fence_reg = I915_FENCE_REG_NONE;
  3043. obj->madv = I915_MADV_WILLNEED;
  3044. /* Avoid an unnecessary call to unbind on the first bind. */
  3045. obj->map_and_fenceable = true;
  3046. i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
  3047. }
  3048. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3049. .get_pages = i915_gem_object_get_pages_gtt,
  3050. .put_pages = i915_gem_object_put_pages_gtt,
  3051. };
  3052. struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
  3053. size_t size)
  3054. {
  3055. struct drm_i915_gem_object *obj;
  3056. struct address_space *mapping;
  3057. u32 mask;
  3058. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  3059. if (obj == NULL)
  3060. return NULL;
  3061. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  3062. kfree(obj);
  3063. return NULL;
  3064. }
  3065. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3066. if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
  3067. /* 965gm cannot relocate objects above 4GiB. */
  3068. mask &= ~__GFP_HIGHMEM;
  3069. mask |= __GFP_DMA32;
  3070. }
  3071. mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  3072. mapping_set_gfp_mask(mapping, mask);
  3073. i915_gem_object_init(obj, &i915_gem_object_ops);
  3074. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3075. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3076. if (HAS_LLC(dev)) {
  3077. /* On some devices, we can have the GPU use the LLC (the CPU
  3078. * cache) for about a 10% performance improvement
  3079. * compared to uncached. Graphics requests other than
  3080. * display scanout are coherent with the CPU in
  3081. * accessing this cache. This means in this mode we
  3082. * don't need to clflush on the CPU side, and on the
  3083. * GPU side we only need to flush internal caches to
  3084. * get data visible to the CPU.
  3085. *
  3086. * However, we maintain the display planes as UC, and so
  3087. * need to rebind when first used as such.
  3088. */
  3089. obj->cache_level = I915_CACHE_LLC;
  3090. } else
  3091. obj->cache_level = I915_CACHE_NONE;
  3092. return obj;
  3093. }
  3094. int i915_gem_init_object(struct drm_gem_object *obj)
  3095. {
  3096. BUG();
  3097. return 0;
  3098. }
  3099. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3100. {
  3101. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3102. struct drm_device *dev = obj->base.dev;
  3103. drm_i915_private_t *dev_priv = dev->dev_private;
  3104. trace_i915_gem_object_destroy(obj);
  3105. if (obj->phys_obj)
  3106. i915_gem_detach_phys_object(dev, obj);
  3107. obj->pin_count = 0;
  3108. if (WARN_ON(i915_gem_object_unbind(obj) == -ERESTARTSYS)) {
  3109. bool was_interruptible;
  3110. was_interruptible = dev_priv->mm.interruptible;
  3111. dev_priv->mm.interruptible = false;
  3112. WARN_ON(i915_gem_object_unbind(obj));
  3113. dev_priv->mm.interruptible = was_interruptible;
  3114. }
  3115. obj->pages_pin_count = 0;
  3116. i915_gem_object_put_pages(obj);
  3117. i915_gem_object_free_mmap_offset(obj);
  3118. BUG_ON(obj->pages);
  3119. if (obj->base.import_attach)
  3120. drm_prime_gem_destroy(&obj->base, NULL);
  3121. drm_gem_object_release(&obj->base);
  3122. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  3123. kfree(obj->bit_17);
  3124. kfree(obj);
  3125. }
  3126. int
  3127. i915_gem_idle(struct drm_device *dev)
  3128. {
  3129. drm_i915_private_t *dev_priv = dev->dev_private;
  3130. int ret;
  3131. mutex_lock(&dev->struct_mutex);
  3132. if (dev_priv->mm.suspended) {
  3133. mutex_unlock(&dev->struct_mutex);
  3134. return 0;
  3135. }
  3136. ret = i915_gpu_idle(dev);
  3137. if (ret) {
  3138. mutex_unlock(&dev->struct_mutex);
  3139. return ret;
  3140. }
  3141. i915_gem_retire_requests(dev);
  3142. /* Under UMS, be paranoid and evict. */
  3143. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3144. i915_gem_evict_everything(dev);
  3145. i915_gem_reset_fences(dev);
  3146. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  3147. * We need to replace this with a semaphore, or something.
  3148. * And not confound mm.suspended!
  3149. */
  3150. dev_priv->mm.suspended = 1;
  3151. del_timer_sync(&dev_priv->hangcheck_timer);
  3152. i915_kernel_lost_context(dev);
  3153. i915_gem_cleanup_ringbuffer(dev);
  3154. mutex_unlock(&dev->struct_mutex);
  3155. /* Cancel the retire work handler, which should be idle now. */
  3156. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  3157. return 0;
  3158. }
  3159. void i915_gem_l3_remap(struct drm_device *dev)
  3160. {
  3161. drm_i915_private_t *dev_priv = dev->dev_private;
  3162. u32 misccpctl;
  3163. int i;
  3164. if (!IS_IVYBRIDGE(dev))
  3165. return;
  3166. if (!dev_priv->l3_parity.remap_info)
  3167. return;
  3168. misccpctl = I915_READ(GEN7_MISCCPCTL);
  3169. I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
  3170. POSTING_READ(GEN7_MISCCPCTL);
  3171. for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
  3172. u32 remap = I915_READ(GEN7_L3LOG_BASE + i);
  3173. if (remap && remap != dev_priv->l3_parity.remap_info[i/4])
  3174. DRM_DEBUG("0x%x was already programmed to %x\n",
  3175. GEN7_L3LOG_BASE + i, remap);
  3176. if (remap && !dev_priv->l3_parity.remap_info[i/4])
  3177. DRM_DEBUG_DRIVER("Clearing remapped register\n");
  3178. I915_WRITE(GEN7_L3LOG_BASE + i, dev_priv->l3_parity.remap_info[i/4]);
  3179. }
  3180. /* Make sure all the writes land before disabling dop clock gating */
  3181. POSTING_READ(GEN7_L3LOG_BASE);
  3182. I915_WRITE(GEN7_MISCCPCTL, misccpctl);
  3183. }
  3184. void i915_gem_init_swizzling(struct drm_device *dev)
  3185. {
  3186. drm_i915_private_t *dev_priv = dev->dev_private;
  3187. if (INTEL_INFO(dev)->gen < 5 ||
  3188. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  3189. return;
  3190. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  3191. DISP_TILE_SURFACE_SWIZZLING);
  3192. if (IS_GEN5(dev))
  3193. return;
  3194. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  3195. if (IS_GEN6(dev))
  3196. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  3197. else
  3198. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  3199. }
  3200. static bool
  3201. intel_enable_blt(struct drm_device *dev)
  3202. {
  3203. if (!HAS_BLT(dev))
  3204. return false;
  3205. /* The blitter was dysfunctional on early prototypes */
  3206. if (IS_GEN6(dev) && dev->pdev->revision < 8) {
  3207. DRM_INFO("BLT not supported on this pre-production hardware;"
  3208. " graphics performance will be degraded.\n");
  3209. return false;
  3210. }
  3211. return true;
  3212. }
  3213. int
  3214. i915_gem_init_hw(struct drm_device *dev)
  3215. {
  3216. drm_i915_private_t *dev_priv = dev->dev_private;
  3217. int ret;
  3218. if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
  3219. return -EIO;
  3220. if (IS_HASWELL(dev) && (I915_READ(0x120010) == 1))
  3221. I915_WRITE(0x9008, I915_READ(0x9008) | 0xf0000);
  3222. i915_gem_l3_remap(dev);
  3223. i915_gem_init_swizzling(dev);
  3224. ret = intel_init_render_ring_buffer(dev);
  3225. if (ret)
  3226. return ret;
  3227. if (HAS_BSD(dev)) {
  3228. ret = intel_init_bsd_ring_buffer(dev);
  3229. if (ret)
  3230. goto cleanup_render_ring;
  3231. }
  3232. if (intel_enable_blt(dev)) {
  3233. ret = intel_init_blt_ring_buffer(dev);
  3234. if (ret)
  3235. goto cleanup_bsd_ring;
  3236. }
  3237. dev_priv->next_seqno = 1;
  3238. /*
  3239. * XXX: There was some w/a described somewhere suggesting loading
  3240. * contexts before PPGTT.
  3241. */
  3242. i915_gem_context_init(dev);
  3243. i915_gem_init_ppgtt(dev);
  3244. return 0;
  3245. cleanup_bsd_ring:
  3246. intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
  3247. cleanup_render_ring:
  3248. intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
  3249. return ret;
  3250. }
  3251. static bool
  3252. intel_enable_ppgtt(struct drm_device *dev)
  3253. {
  3254. if (i915_enable_ppgtt >= 0)
  3255. return i915_enable_ppgtt;
  3256. #ifdef CONFIG_INTEL_IOMMU
  3257. /* Disable ppgtt on SNB if VT-d is on. */
  3258. if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped)
  3259. return false;
  3260. #endif
  3261. return true;
  3262. }
  3263. int i915_gem_init(struct drm_device *dev)
  3264. {
  3265. struct drm_i915_private *dev_priv = dev->dev_private;
  3266. unsigned long gtt_size, mappable_size;
  3267. int ret;
  3268. gtt_size = dev_priv->mm.gtt->gtt_total_entries << PAGE_SHIFT;
  3269. mappable_size = dev_priv->mm.gtt->gtt_mappable_entries << PAGE_SHIFT;
  3270. mutex_lock(&dev->struct_mutex);
  3271. if (intel_enable_ppgtt(dev) && HAS_ALIASING_PPGTT(dev)) {
  3272. /* PPGTT pdes are stolen from global gtt ptes, so shrink the
  3273. * aperture accordingly when using aliasing ppgtt. */
  3274. gtt_size -= I915_PPGTT_PD_ENTRIES*PAGE_SIZE;
  3275. i915_gem_init_global_gtt(dev, 0, mappable_size, gtt_size);
  3276. ret = i915_gem_init_aliasing_ppgtt(dev);
  3277. if (ret) {
  3278. mutex_unlock(&dev->struct_mutex);
  3279. return ret;
  3280. }
  3281. } else {
  3282. /* Let GEM Manage all of the aperture.
  3283. *
  3284. * However, leave one page at the end still bound to the scratch
  3285. * page. There are a number of places where the hardware
  3286. * apparently prefetches past the end of the object, and we've
  3287. * seen multiple hangs with the GPU head pointer stuck in a
  3288. * batchbuffer bound at the last page of the aperture. One page
  3289. * should be enough to keep any prefetching inside of the
  3290. * aperture.
  3291. */
  3292. i915_gem_init_global_gtt(dev, 0, mappable_size,
  3293. gtt_size);
  3294. }
  3295. ret = i915_gem_init_hw(dev);
  3296. mutex_unlock(&dev->struct_mutex);
  3297. if (ret) {
  3298. i915_gem_cleanup_aliasing_ppgtt(dev);
  3299. return ret;
  3300. }
  3301. /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */
  3302. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3303. dev_priv->dri1.allow_batchbuffer = 1;
  3304. return 0;
  3305. }
  3306. void
  3307. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  3308. {
  3309. drm_i915_private_t *dev_priv = dev->dev_private;
  3310. struct intel_ring_buffer *ring;
  3311. int i;
  3312. for_each_ring(ring, dev_priv, i)
  3313. intel_cleanup_ring_buffer(ring);
  3314. }
  3315. int
  3316. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  3317. struct drm_file *file_priv)
  3318. {
  3319. drm_i915_private_t *dev_priv = dev->dev_private;
  3320. int ret;
  3321. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3322. return 0;
  3323. if (atomic_read(&dev_priv->mm.wedged)) {
  3324. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  3325. atomic_set(&dev_priv->mm.wedged, 0);
  3326. }
  3327. mutex_lock(&dev->struct_mutex);
  3328. dev_priv->mm.suspended = 0;
  3329. ret = i915_gem_init_hw(dev);
  3330. if (ret != 0) {
  3331. mutex_unlock(&dev->struct_mutex);
  3332. return ret;
  3333. }
  3334. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  3335. mutex_unlock(&dev->struct_mutex);
  3336. ret = drm_irq_install(dev);
  3337. if (ret)
  3338. goto cleanup_ringbuffer;
  3339. return 0;
  3340. cleanup_ringbuffer:
  3341. mutex_lock(&dev->struct_mutex);
  3342. i915_gem_cleanup_ringbuffer(dev);
  3343. dev_priv->mm.suspended = 1;
  3344. mutex_unlock(&dev->struct_mutex);
  3345. return ret;
  3346. }
  3347. int
  3348. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  3349. struct drm_file *file_priv)
  3350. {
  3351. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3352. return 0;
  3353. drm_irq_uninstall(dev);
  3354. return i915_gem_idle(dev);
  3355. }
  3356. void
  3357. i915_gem_lastclose(struct drm_device *dev)
  3358. {
  3359. int ret;
  3360. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3361. return;
  3362. ret = i915_gem_idle(dev);
  3363. if (ret)
  3364. DRM_ERROR("failed to idle hardware: %d\n", ret);
  3365. }
  3366. static void
  3367. init_ring_lists(struct intel_ring_buffer *ring)
  3368. {
  3369. INIT_LIST_HEAD(&ring->active_list);
  3370. INIT_LIST_HEAD(&ring->request_list);
  3371. }
  3372. void
  3373. i915_gem_load(struct drm_device *dev)
  3374. {
  3375. int i;
  3376. drm_i915_private_t *dev_priv = dev->dev_private;
  3377. INIT_LIST_HEAD(&dev_priv->mm.active_list);
  3378. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  3379. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  3380. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  3381. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  3382. for (i = 0; i < I915_NUM_RINGS; i++)
  3383. init_ring_lists(&dev_priv->ring[i]);
  3384. for (i = 0; i < I915_MAX_NUM_FENCES; i++)
  3385. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  3386. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  3387. i915_gem_retire_work_handler);
  3388. init_completion(&dev_priv->error_completion);
  3389. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  3390. if (IS_GEN3(dev)) {
  3391. I915_WRITE(MI_ARB_STATE,
  3392. _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
  3393. }
  3394. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  3395. /* Old X drivers will take 0-2 for front, back, depth buffers */
  3396. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3397. dev_priv->fence_reg_start = 3;
  3398. if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3399. dev_priv->num_fence_regs = 16;
  3400. else
  3401. dev_priv->num_fence_regs = 8;
  3402. /* Initialize fence registers to zero */
  3403. i915_gem_reset_fences(dev);
  3404. i915_gem_detect_bit_6_swizzle(dev);
  3405. init_waitqueue_head(&dev_priv->pending_flip_queue);
  3406. dev_priv->mm.interruptible = true;
  3407. dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
  3408. dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
  3409. register_shrinker(&dev_priv->mm.inactive_shrinker);
  3410. }
  3411. /*
  3412. * Create a physically contiguous memory object for this object
  3413. * e.g. for cursor + overlay regs
  3414. */
  3415. static int i915_gem_init_phys_object(struct drm_device *dev,
  3416. int id, int size, int align)
  3417. {
  3418. drm_i915_private_t *dev_priv = dev->dev_private;
  3419. struct drm_i915_gem_phys_object *phys_obj;
  3420. int ret;
  3421. if (dev_priv->mm.phys_objs[id - 1] || !size)
  3422. return 0;
  3423. phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
  3424. if (!phys_obj)
  3425. return -ENOMEM;
  3426. phys_obj->id = id;
  3427. phys_obj->handle = drm_pci_alloc(dev, size, align);
  3428. if (!phys_obj->handle) {
  3429. ret = -ENOMEM;
  3430. goto kfree_obj;
  3431. }
  3432. #ifdef CONFIG_X86
  3433. set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3434. #endif
  3435. dev_priv->mm.phys_objs[id - 1] = phys_obj;
  3436. return 0;
  3437. kfree_obj:
  3438. kfree(phys_obj);
  3439. return ret;
  3440. }
  3441. static void i915_gem_free_phys_object(struct drm_device *dev, int id)
  3442. {
  3443. drm_i915_private_t *dev_priv = dev->dev_private;
  3444. struct drm_i915_gem_phys_object *phys_obj;
  3445. if (!dev_priv->mm.phys_objs[id - 1])
  3446. return;
  3447. phys_obj = dev_priv->mm.phys_objs[id - 1];
  3448. if (phys_obj->cur_obj) {
  3449. i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
  3450. }
  3451. #ifdef CONFIG_X86
  3452. set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3453. #endif
  3454. drm_pci_free(dev, phys_obj->handle);
  3455. kfree(phys_obj);
  3456. dev_priv->mm.phys_objs[id - 1] = NULL;
  3457. }
  3458. void i915_gem_free_all_phys_object(struct drm_device *dev)
  3459. {
  3460. int i;
  3461. for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
  3462. i915_gem_free_phys_object(dev, i);
  3463. }
  3464. void i915_gem_detach_phys_object(struct drm_device *dev,
  3465. struct drm_i915_gem_object *obj)
  3466. {
  3467. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  3468. char *vaddr;
  3469. int i;
  3470. int page_count;
  3471. if (!obj->phys_obj)
  3472. return;
  3473. vaddr = obj->phys_obj->handle->vaddr;
  3474. page_count = obj->base.size / PAGE_SIZE;
  3475. for (i = 0; i < page_count; i++) {
  3476. struct page *page = shmem_read_mapping_page(mapping, i);
  3477. if (!IS_ERR(page)) {
  3478. char *dst = kmap_atomic(page);
  3479. memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
  3480. kunmap_atomic(dst);
  3481. drm_clflush_pages(&page, 1);
  3482. set_page_dirty(page);
  3483. mark_page_accessed(page);
  3484. page_cache_release(page);
  3485. }
  3486. }
  3487. i915_gem_chipset_flush(dev);
  3488. obj->phys_obj->cur_obj = NULL;
  3489. obj->phys_obj = NULL;
  3490. }
  3491. int
  3492. i915_gem_attach_phys_object(struct drm_device *dev,
  3493. struct drm_i915_gem_object *obj,
  3494. int id,
  3495. int align)
  3496. {
  3497. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  3498. drm_i915_private_t *dev_priv = dev->dev_private;
  3499. int ret = 0;
  3500. int page_count;
  3501. int i;
  3502. if (id > I915_MAX_PHYS_OBJECT)
  3503. return -EINVAL;
  3504. if (obj->phys_obj) {
  3505. if (obj->phys_obj->id == id)
  3506. return 0;
  3507. i915_gem_detach_phys_object(dev, obj);
  3508. }
  3509. /* create a new object */
  3510. if (!dev_priv->mm.phys_objs[id - 1]) {
  3511. ret = i915_gem_init_phys_object(dev, id,
  3512. obj->base.size, align);
  3513. if (ret) {
  3514. DRM_ERROR("failed to init phys object %d size: %zu\n",
  3515. id, obj->base.size);
  3516. return ret;
  3517. }
  3518. }
  3519. /* bind to the object */
  3520. obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
  3521. obj->phys_obj->cur_obj = obj;
  3522. page_count = obj->base.size / PAGE_SIZE;
  3523. for (i = 0; i < page_count; i++) {
  3524. struct page *page;
  3525. char *dst, *src;
  3526. page = shmem_read_mapping_page(mapping, i);
  3527. if (IS_ERR(page))
  3528. return PTR_ERR(page);
  3529. src = kmap_atomic(page);
  3530. dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  3531. memcpy(dst, src, PAGE_SIZE);
  3532. kunmap_atomic(src);
  3533. mark_page_accessed(page);
  3534. page_cache_release(page);
  3535. }
  3536. return 0;
  3537. }
  3538. static int
  3539. i915_gem_phys_pwrite(struct drm_device *dev,
  3540. struct drm_i915_gem_object *obj,
  3541. struct drm_i915_gem_pwrite *args,
  3542. struct drm_file *file_priv)
  3543. {
  3544. void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
  3545. char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
  3546. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  3547. unsigned long unwritten;
  3548. /* The physical object once assigned is fixed for the lifetime
  3549. * of the obj, so we can safely drop the lock and continue
  3550. * to access vaddr.
  3551. */
  3552. mutex_unlock(&dev->struct_mutex);
  3553. unwritten = copy_from_user(vaddr, user_data, args->size);
  3554. mutex_lock(&dev->struct_mutex);
  3555. if (unwritten)
  3556. return -EFAULT;
  3557. }
  3558. i915_gem_chipset_flush(dev);
  3559. return 0;
  3560. }
  3561. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  3562. {
  3563. struct drm_i915_file_private *file_priv = file->driver_priv;
  3564. /* Clean up our request list when the client is going away, so that
  3565. * later retire_requests won't dereference our soon-to-be-gone
  3566. * file_priv.
  3567. */
  3568. spin_lock(&file_priv->mm.lock);
  3569. while (!list_empty(&file_priv->mm.request_list)) {
  3570. struct drm_i915_gem_request *request;
  3571. request = list_first_entry(&file_priv->mm.request_list,
  3572. struct drm_i915_gem_request,
  3573. client_list);
  3574. list_del(&request->client_list);
  3575. request->file_priv = NULL;
  3576. }
  3577. spin_unlock(&file_priv->mm.lock);
  3578. }
  3579. static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
  3580. {
  3581. if (!mutex_is_locked(mutex))
  3582. return false;
  3583. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)
  3584. return mutex->owner == task;
  3585. #else
  3586. /* Since UP may be pre-empted, we cannot assume that we own the lock */
  3587. return false;
  3588. #endif
  3589. }
  3590. static int
  3591. i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
  3592. {
  3593. struct drm_i915_private *dev_priv =
  3594. container_of(shrinker,
  3595. struct drm_i915_private,
  3596. mm.inactive_shrinker);
  3597. struct drm_device *dev = dev_priv->dev;
  3598. struct drm_i915_gem_object *obj;
  3599. int nr_to_scan = sc->nr_to_scan;
  3600. bool unlock = true;
  3601. int cnt;
  3602. if (!mutex_trylock(&dev->struct_mutex)) {
  3603. if (!mutex_is_locked_by(&dev->struct_mutex, current))
  3604. return 0;
  3605. unlock = false;
  3606. }
  3607. if (nr_to_scan) {
  3608. nr_to_scan -= i915_gem_purge(dev_priv, nr_to_scan);
  3609. if (nr_to_scan > 0)
  3610. i915_gem_shrink_all(dev_priv);
  3611. }
  3612. cnt = 0;
  3613. list_for_each_entry(obj, &dev_priv->mm.unbound_list, gtt_list)
  3614. if (obj->pages_pin_count == 0)
  3615. cnt += obj->base.size >> PAGE_SHIFT;
  3616. list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
  3617. if (obj->pin_count == 0 && obj->pages_pin_count == 0)
  3618. cnt += obj->base.size >> PAGE_SHIFT;
  3619. if (unlock)
  3620. mutex_unlock(&dev->struct_mutex);
  3621. return cnt;
  3622. }