i915_gem.c 109 KB

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