i915_gem.c 105 KB

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