i915_gem.c 115 KB

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