i915_gem.c 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663
  1. /*
  2. * Copyright © 2008 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/i915_drm.h>
  29. #include "i915_drv.h"
  30. #include "i915_trace.h"
  31. #include "intel_drv.h"
  32. #include <linux/shmem_fs.h>
  33. #include <linux/slab.h>
  34. #include <linux/swap.h>
  35. #include <linux/pci.h>
  36. #include <linux/dma-buf.h>
  37. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  38. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
  39. static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
  40. unsigned alignment,
  41. bool map_and_fenceable,
  42. bool nonblocking);
  43. static int i915_gem_phys_pwrite(struct drm_device *dev,
  44. struct drm_i915_gem_object *obj,
  45. struct drm_i915_gem_pwrite *args,
  46. struct drm_file *file);
  47. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  48. struct drm_i915_gem_object *obj);
  49. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  50. struct drm_i915_fence_reg *fence,
  51. bool enable);
  52. static int i915_gem_inactive_shrink(struct shrinker *shrinker,
  53. struct shrink_control *sc);
  54. static long i915_gem_purge(struct drm_i915_private *dev_priv, long target);
  55. static void i915_gem_shrink_all(struct drm_i915_private *dev_priv);
  56. static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
  57. static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
  58. {
  59. if (obj->tiling_mode)
  60. i915_gem_release_mmap(obj);
  61. /* As we do not have an associated fence register, we will force
  62. * a tiling change if we ever need to acquire one.
  63. */
  64. obj->fence_dirty = false;
  65. obj->fence_reg = I915_FENCE_REG_NONE;
  66. }
  67. /* some bookkeeping */
  68. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  69. size_t size)
  70. {
  71. spin_lock(&dev_priv->mm.object_stat_lock);
  72. dev_priv->mm.object_count++;
  73. dev_priv->mm.object_memory += size;
  74. spin_unlock(&dev_priv->mm.object_stat_lock);
  75. }
  76. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  77. size_t size)
  78. {
  79. spin_lock(&dev_priv->mm.object_stat_lock);
  80. dev_priv->mm.object_count--;
  81. dev_priv->mm.object_memory -= size;
  82. spin_unlock(&dev_priv->mm.object_stat_lock);
  83. }
  84. static int
  85. i915_gem_wait_for_error(struct i915_gpu_error *error)
  86. {
  87. int ret;
  88. #define EXIT_COND (!i915_reset_in_progress(error) || \
  89. i915_terminally_wedged(error))
  90. if (EXIT_COND)
  91. return 0;
  92. /*
  93. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  94. * userspace. If it takes that long something really bad is going on and
  95. * we should simply try to bail out and fail as gracefully as possible.
  96. */
  97. ret = wait_event_interruptible_timeout(error->reset_queue,
  98. EXIT_COND,
  99. 10*HZ);
  100. if (ret == 0) {
  101. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  102. return -EIO;
  103. } else if (ret < 0) {
  104. return ret;
  105. }
  106. #undef EXIT_COND
  107. return 0;
  108. }
  109. int i915_mutex_lock_interruptible(struct drm_device *dev)
  110. {
  111. struct drm_i915_private *dev_priv = dev->dev_private;
  112. int ret;
  113. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  114. if (ret)
  115. return ret;
  116. ret = mutex_lock_interruptible(&dev->struct_mutex);
  117. if (ret)
  118. return ret;
  119. WARN_ON(i915_verify_lists(dev));
  120. return 0;
  121. }
  122. static inline bool
  123. i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
  124. {
  125. return i915_gem_obj_ggtt_bound(obj) && !obj->active;
  126. }
  127. int
  128. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  129. struct drm_file *file)
  130. {
  131. struct drm_i915_private *dev_priv = dev->dev_private;
  132. struct drm_i915_gem_init *args = data;
  133. if (drm_core_check_feature(dev, DRIVER_MODESET))
  134. return -ENODEV;
  135. if (args->gtt_start >= args->gtt_end ||
  136. (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
  137. return -EINVAL;
  138. /* GEM with user mode setting was never supported on ilk and later. */
  139. if (INTEL_INFO(dev)->gen >= 5)
  140. return -ENODEV;
  141. mutex_lock(&dev->struct_mutex);
  142. i915_gem_setup_global_gtt(dev, args->gtt_start, args->gtt_end,
  143. args->gtt_end);
  144. dev_priv->gtt.mappable_end = args->gtt_end;
  145. mutex_unlock(&dev->struct_mutex);
  146. return 0;
  147. }
  148. int
  149. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  150. struct drm_file *file)
  151. {
  152. struct drm_i915_private *dev_priv = dev->dev_private;
  153. struct drm_i915_gem_get_aperture *args = data;
  154. struct drm_i915_gem_object *obj;
  155. size_t pinned;
  156. pinned = 0;
  157. mutex_lock(&dev->struct_mutex);
  158. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  159. if (obj->pin_count)
  160. pinned += i915_gem_obj_ggtt_size(obj);
  161. mutex_unlock(&dev->struct_mutex);
  162. args->aper_size = dev_priv->gtt.base.total;
  163. args->aper_available_size = args->aper_size - pinned;
  164. return 0;
  165. }
  166. void *i915_gem_object_alloc(struct drm_device *dev)
  167. {
  168. struct drm_i915_private *dev_priv = dev->dev_private;
  169. return kmem_cache_alloc(dev_priv->slab, GFP_KERNEL | __GFP_ZERO);
  170. }
  171. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  172. {
  173. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  174. kmem_cache_free(dev_priv->slab, obj);
  175. }
  176. static int
  177. i915_gem_create(struct drm_file *file,
  178. struct drm_device *dev,
  179. uint64_t size,
  180. uint32_t *handle_p)
  181. {
  182. struct drm_i915_gem_object *obj;
  183. int ret;
  184. u32 handle;
  185. size = roundup(size, PAGE_SIZE);
  186. if (size == 0)
  187. return -EINVAL;
  188. /* Allocate the new object */
  189. obj = i915_gem_alloc_object(dev, size);
  190. if (obj == NULL)
  191. return -ENOMEM;
  192. ret = drm_gem_handle_create(file, &obj->base, &handle);
  193. /* drop reference from allocate - handle holds it now */
  194. drm_gem_object_unreference_unlocked(&obj->base);
  195. if (ret)
  196. return ret;
  197. *handle_p = handle;
  198. return 0;
  199. }
  200. int
  201. i915_gem_dumb_create(struct drm_file *file,
  202. struct drm_device *dev,
  203. struct drm_mode_create_dumb *args)
  204. {
  205. /* have to work out size/pitch and return them */
  206. args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
  207. args->size = args->pitch * args->height;
  208. return i915_gem_create(file, dev,
  209. args->size, &args->handle);
  210. }
  211. int i915_gem_dumb_destroy(struct drm_file *file,
  212. struct drm_device *dev,
  213. uint32_t handle)
  214. {
  215. return drm_gem_handle_delete(file, handle);
  216. }
  217. /**
  218. * Creates a new mm object and returns a handle to it.
  219. */
  220. int
  221. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  222. struct drm_file *file)
  223. {
  224. struct drm_i915_gem_create *args = data;
  225. return i915_gem_create(file, dev,
  226. args->size, &args->handle);
  227. }
  228. static inline int
  229. __copy_to_user_swizzled(char __user *cpu_vaddr,
  230. const char *gpu_vaddr, int gpu_offset,
  231. int length)
  232. {
  233. int ret, cpu_offset = 0;
  234. while (length > 0) {
  235. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  236. int this_length = min(cacheline_end - gpu_offset, length);
  237. int swizzled_gpu_offset = gpu_offset ^ 64;
  238. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  239. gpu_vaddr + swizzled_gpu_offset,
  240. this_length);
  241. if (ret)
  242. return ret + length;
  243. cpu_offset += this_length;
  244. gpu_offset += this_length;
  245. length -= this_length;
  246. }
  247. return 0;
  248. }
  249. static inline int
  250. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  251. const char __user *cpu_vaddr,
  252. int length)
  253. {
  254. int ret, cpu_offset = 0;
  255. while (length > 0) {
  256. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  257. int this_length = min(cacheline_end - gpu_offset, length);
  258. int swizzled_gpu_offset = gpu_offset ^ 64;
  259. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  260. cpu_vaddr + cpu_offset,
  261. this_length);
  262. if (ret)
  263. return ret + length;
  264. cpu_offset += this_length;
  265. gpu_offset += this_length;
  266. length -= this_length;
  267. }
  268. return 0;
  269. }
  270. /* Per-page copy function for the shmem pread fastpath.
  271. * Flushes invalid cachelines before reading the target if
  272. * needs_clflush is set. */
  273. static int
  274. shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
  275. char __user *user_data,
  276. bool page_do_bit17_swizzling, bool needs_clflush)
  277. {
  278. char *vaddr;
  279. int ret;
  280. if (unlikely(page_do_bit17_swizzling))
  281. return -EINVAL;
  282. vaddr = kmap_atomic(page);
  283. if (needs_clflush)
  284. drm_clflush_virt_range(vaddr + shmem_page_offset,
  285. page_length);
  286. ret = __copy_to_user_inatomic(user_data,
  287. vaddr + shmem_page_offset,
  288. page_length);
  289. kunmap_atomic(vaddr);
  290. return ret ? -EFAULT : 0;
  291. }
  292. static void
  293. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  294. bool swizzled)
  295. {
  296. if (unlikely(swizzled)) {
  297. unsigned long start = (unsigned long) addr;
  298. unsigned long end = (unsigned long) addr + length;
  299. /* For swizzling simply ensure that we always flush both
  300. * channels. Lame, but simple and it works. Swizzled
  301. * pwrite/pread is far from a hotpath - current userspace
  302. * doesn't use it at all. */
  303. start = round_down(start, 128);
  304. end = round_up(end, 128);
  305. drm_clflush_virt_range((void *)start, end - start);
  306. } else {
  307. drm_clflush_virt_range(addr, length);
  308. }
  309. }
  310. /* Only difference to the fast-path function is that this can handle bit17
  311. * and uses non-atomic copy and kmap functions. */
  312. static int
  313. shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
  314. char __user *user_data,
  315. bool page_do_bit17_swizzling, bool needs_clflush)
  316. {
  317. char *vaddr;
  318. int ret;
  319. vaddr = kmap(page);
  320. if (needs_clflush)
  321. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  322. page_length,
  323. page_do_bit17_swizzling);
  324. if (page_do_bit17_swizzling)
  325. ret = __copy_to_user_swizzled(user_data,
  326. vaddr, shmem_page_offset,
  327. page_length);
  328. else
  329. ret = __copy_to_user(user_data,
  330. vaddr + shmem_page_offset,
  331. page_length);
  332. kunmap(page);
  333. return ret ? - EFAULT : 0;
  334. }
  335. static int
  336. i915_gem_shmem_pread(struct drm_device *dev,
  337. struct drm_i915_gem_object *obj,
  338. struct drm_i915_gem_pread *args,
  339. struct drm_file *file)
  340. {
  341. char __user *user_data;
  342. ssize_t remain;
  343. loff_t offset;
  344. int shmem_page_offset, page_length, ret = 0;
  345. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  346. int prefaulted = 0;
  347. int needs_clflush = 0;
  348. struct sg_page_iter sg_iter;
  349. user_data = to_user_ptr(args->data_ptr);
  350. remain = args->size;
  351. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  352. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
  353. /* If we're not in the cpu read domain, set ourself into the gtt
  354. * read domain and manually flush cachelines (if required). This
  355. * optimizes for the case when the gpu will dirty the data
  356. * anyway again before the next pread happens. */
  357. if (obj->cache_level == I915_CACHE_NONE)
  358. needs_clflush = 1;
  359. if (i915_gem_obj_ggtt_bound(obj)) {
  360. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  361. if (ret)
  362. return ret;
  363. }
  364. }
  365. ret = i915_gem_object_get_pages(obj);
  366. if (ret)
  367. return ret;
  368. i915_gem_object_pin_pages(obj);
  369. offset = args->offset;
  370. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  371. offset >> PAGE_SHIFT) {
  372. struct page *page = sg_page_iter_page(&sg_iter);
  373. if (remain <= 0)
  374. break;
  375. /* Operation in this page
  376. *
  377. * shmem_page_offset = offset within page in shmem file
  378. * page_length = bytes to copy for this page
  379. */
  380. shmem_page_offset = offset_in_page(offset);
  381. page_length = remain;
  382. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  383. page_length = PAGE_SIZE - shmem_page_offset;
  384. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  385. (page_to_phys(page) & (1 << 17)) != 0;
  386. ret = shmem_pread_fast(page, shmem_page_offset, page_length,
  387. user_data, page_do_bit17_swizzling,
  388. needs_clflush);
  389. if (ret == 0)
  390. goto next_page;
  391. mutex_unlock(&dev->struct_mutex);
  392. if (likely(!i915_prefault_disable) && !prefaulted) {
  393. ret = fault_in_multipages_writeable(user_data, remain);
  394. /* Userspace is tricking us, but we've already clobbered
  395. * its pages with the prefault and promised to write the
  396. * data up to the first fault. Hence ignore any errors
  397. * and just continue. */
  398. (void)ret;
  399. prefaulted = 1;
  400. }
  401. ret = shmem_pread_slow(page, shmem_page_offset, page_length,
  402. user_data, page_do_bit17_swizzling,
  403. needs_clflush);
  404. mutex_lock(&dev->struct_mutex);
  405. next_page:
  406. mark_page_accessed(page);
  407. if (ret)
  408. goto out;
  409. remain -= page_length;
  410. user_data += page_length;
  411. offset += page_length;
  412. }
  413. out:
  414. i915_gem_object_unpin_pages(obj);
  415. return ret;
  416. }
  417. /**
  418. * Reads data from the object referenced by handle.
  419. *
  420. * On error, the contents of *data are undefined.
  421. */
  422. int
  423. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  424. struct drm_file *file)
  425. {
  426. struct drm_i915_gem_pread *args = data;
  427. struct drm_i915_gem_object *obj;
  428. int ret = 0;
  429. if (args->size == 0)
  430. return 0;
  431. if (!access_ok(VERIFY_WRITE,
  432. to_user_ptr(args->data_ptr),
  433. args->size))
  434. return -EFAULT;
  435. ret = i915_mutex_lock_interruptible(dev);
  436. if (ret)
  437. return ret;
  438. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  439. if (&obj->base == NULL) {
  440. ret = -ENOENT;
  441. goto unlock;
  442. }
  443. /* Bounds check source. */
  444. if (args->offset > obj->base.size ||
  445. args->size > obj->base.size - args->offset) {
  446. ret = -EINVAL;
  447. goto out;
  448. }
  449. /* prime objects have no backing filp to GEM pread/pwrite
  450. * pages from.
  451. */
  452. if (!obj->base.filp) {
  453. ret = -EINVAL;
  454. goto out;
  455. }
  456. trace_i915_gem_object_pread(obj, args->offset, args->size);
  457. ret = i915_gem_shmem_pread(dev, obj, args, file);
  458. out:
  459. drm_gem_object_unreference(&obj->base);
  460. unlock:
  461. mutex_unlock(&dev->struct_mutex);
  462. return ret;
  463. }
  464. /* This is the fast write path which cannot handle
  465. * page faults in the source data
  466. */
  467. static inline int
  468. fast_user_write(struct io_mapping *mapping,
  469. loff_t page_base, int page_offset,
  470. char __user *user_data,
  471. int length)
  472. {
  473. void __iomem *vaddr_atomic;
  474. void *vaddr;
  475. unsigned long unwritten;
  476. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  477. /* We can use the cpu mem copy function because this is X86. */
  478. vaddr = (void __force*)vaddr_atomic + page_offset;
  479. unwritten = __copy_from_user_inatomic_nocache(vaddr,
  480. user_data, length);
  481. io_mapping_unmap_atomic(vaddr_atomic);
  482. return unwritten;
  483. }
  484. /**
  485. * This is the fast pwrite path, where we copy the data directly from the
  486. * user into the GTT, uncached.
  487. */
  488. static int
  489. i915_gem_gtt_pwrite_fast(struct drm_device *dev,
  490. struct drm_i915_gem_object *obj,
  491. struct drm_i915_gem_pwrite *args,
  492. struct drm_file *file)
  493. {
  494. drm_i915_private_t *dev_priv = dev->dev_private;
  495. ssize_t remain;
  496. loff_t offset, page_base;
  497. char __user *user_data;
  498. int page_offset, page_length, ret;
  499. ret = i915_gem_object_pin(obj, 0, true, true);
  500. if (ret)
  501. goto out;
  502. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  503. if (ret)
  504. goto out_unpin;
  505. ret = i915_gem_object_put_fence(obj);
  506. if (ret)
  507. goto out_unpin;
  508. user_data = to_user_ptr(args->data_ptr);
  509. remain = args->size;
  510. offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
  511. while (remain > 0) {
  512. /* Operation in this page
  513. *
  514. * page_base = page offset within aperture
  515. * page_offset = offset within page
  516. * page_length = bytes to copy for this page
  517. */
  518. page_base = offset & PAGE_MASK;
  519. page_offset = offset_in_page(offset);
  520. page_length = remain;
  521. if ((page_offset + remain) > PAGE_SIZE)
  522. page_length = PAGE_SIZE - page_offset;
  523. /* If we get a fault while copying data, then (presumably) our
  524. * source page isn't available. Return the error and we'll
  525. * retry in the slow path.
  526. */
  527. if (fast_user_write(dev_priv->gtt.mappable, page_base,
  528. page_offset, user_data, page_length)) {
  529. ret = -EFAULT;
  530. goto out_unpin;
  531. }
  532. remain -= page_length;
  533. user_data += page_length;
  534. offset += page_length;
  535. }
  536. out_unpin:
  537. i915_gem_object_unpin(obj);
  538. out:
  539. return ret;
  540. }
  541. /* Per-page copy function for the shmem pwrite fastpath.
  542. * Flushes invalid cachelines before writing to the target if
  543. * needs_clflush_before is set and flushes out any written cachelines after
  544. * writing if needs_clflush is set. */
  545. static int
  546. shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
  547. char __user *user_data,
  548. bool page_do_bit17_swizzling,
  549. bool needs_clflush_before,
  550. bool needs_clflush_after)
  551. {
  552. char *vaddr;
  553. int ret;
  554. if (unlikely(page_do_bit17_swizzling))
  555. return -EINVAL;
  556. vaddr = kmap_atomic(page);
  557. if (needs_clflush_before)
  558. drm_clflush_virt_range(vaddr + shmem_page_offset,
  559. page_length);
  560. ret = __copy_from_user_inatomic_nocache(vaddr + shmem_page_offset,
  561. user_data,
  562. page_length);
  563. if (needs_clflush_after)
  564. drm_clflush_virt_range(vaddr + shmem_page_offset,
  565. page_length);
  566. kunmap_atomic(vaddr);
  567. return ret ? -EFAULT : 0;
  568. }
  569. /* Only difference to the fast-path function is that this can handle bit17
  570. * and uses non-atomic copy and kmap functions. */
  571. static int
  572. shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
  573. char __user *user_data,
  574. bool page_do_bit17_swizzling,
  575. bool needs_clflush_before,
  576. bool needs_clflush_after)
  577. {
  578. char *vaddr;
  579. int ret;
  580. vaddr = kmap(page);
  581. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  582. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  583. page_length,
  584. page_do_bit17_swizzling);
  585. if (page_do_bit17_swizzling)
  586. ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
  587. user_data,
  588. page_length);
  589. else
  590. ret = __copy_from_user(vaddr + shmem_page_offset,
  591. user_data,
  592. page_length);
  593. if (needs_clflush_after)
  594. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  595. page_length,
  596. page_do_bit17_swizzling);
  597. kunmap(page);
  598. return ret ? -EFAULT : 0;
  599. }
  600. static int
  601. i915_gem_shmem_pwrite(struct drm_device *dev,
  602. struct drm_i915_gem_object *obj,
  603. struct drm_i915_gem_pwrite *args,
  604. struct drm_file *file)
  605. {
  606. ssize_t remain;
  607. loff_t offset;
  608. char __user *user_data;
  609. int shmem_page_offset, page_length, ret = 0;
  610. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  611. int hit_slowpath = 0;
  612. int needs_clflush_after = 0;
  613. int needs_clflush_before = 0;
  614. struct sg_page_iter sg_iter;
  615. user_data = to_user_ptr(args->data_ptr);
  616. remain = args->size;
  617. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  618. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  619. /* If we're not in the cpu write domain, set ourself into the gtt
  620. * write domain and manually flush cachelines (if required). This
  621. * optimizes for the case when the gpu will use the data
  622. * right away and we therefore have to clflush anyway. */
  623. if (obj->cache_level == I915_CACHE_NONE)
  624. needs_clflush_after = 1;
  625. if (i915_gem_obj_ggtt_bound(obj)) {
  626. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  627. if (ret)
  628. return ret;
  629. }
  630. }
  631. /* Same trick applies for invalidate partially written cachelines before
  632. * writing. */
  633. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)
  634. && obj->cache_level == I915_CACHE_NONE)
  635. needs_clflush_before = 1;
  636. ret = i915_gem_object_get_pages(obj);
  637. if (ret)
  638. return ret;
  639. i915_gem_object_pin_pages(obj);
  640. offset = args->offset;
  641. obj->dirty = 1;
  642. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  643. offset >> PAGE_SHIFT) {
  644. struct page *page = sg_page_iter_page(&sg_iter);
  645. int partial_cacheline_write;
  646. if (remain <= 0)
  647. break;
  648. /* Operation in this page
  649. *
  650. * shmem_page_offset = offset within page in shmem file
  651. * page_length = bytes to copy for this page
  652. */
  653. shmem_page_offset = offset_in_page(offset);
  654. page_length = remain;
  655. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  656. page_length = PAGE_SIZE - shmem_page_offset;
  657. /* If we don't overwrite a cacheline completely we need to be
  658. * careful to have up-to-date data by first clflushing. Don't
  659. * overcomplicate things and flush the entire patch. */
  660. partial_cacheline_write = needs_clflush_before &&
  661. ((shmem_page_offset | page_length)
  662. & (boot_cpu_data.x86_clflush_size - 1));
  663. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  664. (page_to_phys(page) & (1 << 17)) != 0;
  665. ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
  666. user_data, page_do_bit17_swizzling,
  667. partial_cacheline_write,
  668. needs_clflush_after);
  669. if (ret == 0)
  670. goto next_page;
  671. hit_slowpath = 1;
  672. mutex_unlock(&dev->struct_mutex);
  673. ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
  674. user_data, page_do_bit17_swizzling,
  675. partial_cacheline_write,
  676. needs_clflush_after);
  677. mutex_lock(&dev->struct_mutex);
  678. next_page:
  679. set_page_dirty(page);
  680. mark_page_accessed(page);
  681. if (ret)
  682. goto out;
  683. remain -= page_length;
  684. user_data += page_length;
  685. offset += page_length;
  686. }
  687. out:
  688. i915_gem_object_unpin_pages(obj);
  689. if (hit_slowpath) {
  690. /*
  691. * Fixup: Flush cpu caches in case we didn't flush the dirty
  692. * cachelines in-line while writing and the object moved
  693. * out of the cpu write domain while we've dropped the lock.
  694. */
  695. if (!needs_clflush_after &&
  696. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  697. i915_gem_clflush_object(obj);
  698. i915_gem_chipset_flush(dev);
  699. }
  700. }
  701. if (needs_clflush_after)
  702. i915_gem_chipset_flush(dev);
  703. return ret;
  704. }
  705. /**
  706. * Writes data to the object referenced by handle.
  707. *
  708. * On error, the contents of the buffer that were to be modified are undefined.
  709. */
  710. int
  711. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  712. struct drm_file *file)
  713. {
  714. struct drm_i915_gem_pwrite *args = data;
  715. struct drm_i915_gem_object *obj;
  716. int ret;
  717. if (args->size == 0)
  718. return 0;
  719. if (!access_ok(VERIFY_READ,
  720. to_user_ptr(args->data_ptr),
  721. args->size))
  722. return -EFAULT;
  723. if (likely(!i915_prefault_disable)) {
  724. ret = fault_in_multipages_readable(to_user_ptr(args->data_ptr),
  725. args->size);
  726. if (ret)
  727. return -EFAULT;
  728. }
  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)obj->base.map_list.hash.key<<PAGE_SHIFT,
  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 (obj->base.map_list.map)
  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. if (!obj->base.map_list.map)
  1283. return;
  1284. drm_gem_free_mmap_offset(&obj->base);
  1285. }
  1286. int
  1287. i915_gem_mmap_gtt(struct drm_file *file,
  1288. struct drm_device *dev,
  1289. uint32_t handle,
  1290. uint64_t *offset)
  1291. {
  1292. struct drm_i915_private *dev_priv = dev->dev_private;
  1293. struct drm_i915_gem_object *obj;
  1294. int ret;
  1295. ret = i915_mutex_lock_interruptible(dev);
  1296. if (ret)
  1297. return ret;
  1298. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  1299. if (&obj->base == NULL) {
  1300. ret = -ENOENT;
  1301. goto unlock;
  1302. }
  1303. if (obj->base.size > dev_priv->gtt.mappable_end) {
  1304. ret = -E2BIG;
  1305. goto out;
  1306. }
  1307. if (obj->madv != I915_MADV_WILLNEED) {
  1308. DRM_ERROR("Attempting to mmap a purgeable buffer\n");
  1309. ret = -EINVAL;
  1310. goto out;
  1311. }
  1312. ret = i915_gem_object_create_mmap_offset(obj);
  1313. if (ret)
  1314. goto out;
  1315. *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
  1316. out:
  1317. drm_gem_object_unreference(&obj->base);
  1318. unlock:
  1319. mutex_unlock(&dev->struct_mutex);
  1320. return ret;
  1321. }
  1322. /**
  1323. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1324. * @dev: DRM device
  1325. * @data: GTT mapping ioctl data
  1326. * @file: GEM object info
  1327. *
  1328. * Simply returns the fake offset to userspace so it can mmap it.
  1329. * The mmap call will end up in drm_gem_mmap(), which will set things
  1330. * up so we can get faults in the handler above.
  1331. *
  1332. * The fault handler will take care of binding the object into the GTT
  1333. * (since it may have been evicted to make room for something), allocating
  1334. * a fence register, and mapping the appropriate aperture address into
  1335. * userspace.
  1336. */
  1337. int
  1338. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1339. struct drm_file *file)
  1340. {
  1341. struct drm_i915_gem_mmap_gtt *args = data;
  1342. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1343. }
  1344. /* Immediately discard the backing storage */
  1345. static void
  1346. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1347. {
  1348. struct inode *inode;
  1349. i915_gem_object_free_mmap_offset(obj);
  1350. if (obj->base.filp == NULL)
  1351. return;
  1352. /* Our goal here is to return as much of the memory as
  1353. * is possible back to the system as we are called from OOM.
  1354. * To do this we must instruct the shmfs to drop all of its
  1355. * backing pages, *now*.
  1356. */
  1357. inode = file_inode(obj->base.filp);
  1358. shmem_truncate_range(inode, 0, (loff_t)-1);
  1359. obj->madv = __I915_MADV_PURGED;
  1360. }
  1361. static inline int
  1362. i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
  1363. {
  1364. return obj->madv == I915_MADV_DONTNEED;
  1365. }
  1366. static void
  1367. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  1368. {
  1369. struct sg_page_iter sg_iter;
  1370. int ret;
  1371. BUG_ON(obj->madv == __I915_MADV_PURGED);
  1372. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  1373. if (ret) {
  1374. /* In the event of a disaster, abandon all caches and
  1375. * hope for the best.
  1376. */
  1377. WARN_ON(ret != -EIO);
  1378. i915_gem_clflush_object(obj);
  1379. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  1380. }
  1381. if (i915_gem_object_needs_bit17_swizzle(obj))
  1382. i915_gem_object_save_bit_17_swizzle(obj);
  1383. if (obj->madv == I915_MADV_DONTNEED)
  1384. obj->dirty = 0;
  1385. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  1386. struct page *page = sg_page_iter_page(&sg_iter);
  1387. if (obj->dirty)
  1388. set_page_dirty(page);
  1389. if (obj->madv == I915_MADV_WILLNEED)
  1390. mark_page_accessed(page);
  1391. page_cache_release(page);
  1392. }
  1393. obj->dirty = 0;
  1394. sg_free_table(obj->pages);
  1395. kfree(obj->pages);
  1396. }
  1397. int
  1398. i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
  1399. {
  1400. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1401. if (obj->pages == NULL)
  1402. return 0;
  1403. BUG_ON(i915_gem_obj_ggtt_bound(obj));
  1404. if (obj->pages_pin_count)
  1405. return -EBUSY;
  1406. /* ->put_pages might need to allocate memory for the bit17 swizzle
  1407. * array, hence protect them from being reaped by removing them from gtt
  1408. * lists early. */
  1409. list_del(&obj->global_list);
  1410. ops->put_pages(obj);
  1411. obj->pages = NULL;
  1412. if (i915_gem_object_is_purgeable(obj))
  1413. i915_gem_object_truncate(obj);
  1414. return 0;
  1415. }
  1416. static long
  1417. __i915_gem_shrink(struct drm_i915_private *dev_priv, long target,
  1418. bool purgeable_only)
  1419. {
  1420. struct drm_i915_gem_object *obj, *next;
  1421. struct i915_address_space *vm = &dev_priv->gtt.base;
  1422. long count = 0;
  1423. list_for_each_entry_safe(obj, next,
  1424. &dev_priv->mm.unbound_list,
  1425. global_list) {
  1426. if ((i915_gem_object_is_purgeable(obj) || !purgeable_only) &&
  1427. i915_gem_object_put_pages(obj) == 0) {
  1428. count += obj->base.size >> PAGE_SHIFT;
  1429. if (count >= target)
  1430. return count;
  1431. }
  1432. }
  1433. list_for_each_entry_safe(obj, next, &vm->inactive_list, mm_list) {
  1434. if ((i915_gem_object_is_purgeable(obj) || !purgeable_only) &&
  1435. i915_gem_object_unbind(obj) == 0 &&
  1436. i915_gem_object_put_pages(obj) == 0) {
  1437. count += obj->base.size >> PAGE_SHIFT;
  1438. if (count >= target)
  1439. return count;
  1440. }
  1441. }
  1442. return count;
  1443. }
  1444. static long
  1445. i915_gem_purge(struct drm_i915_private *dev_priv, long target)
  1446. {
  1447. return __i915_gem_shrink(dev_priv, target, true);
  1448. }
  1449. static void
  1450. i915_gem_shrink_all(struct drm_i915_private *dev_priv)
  1451. {
  1452. struct drm_i915_gem_object *obj, *next;
  1453. i915_gem_evict_everything(dev_priv->dev);
  1454. list_for_each_entry_safe(obj, next, &dev_priv->mm.unbound_list,
  1455. global_list)
  1456. i915_gem_object_put_pages(obj);
  1457. }
  1458. static int
  1459. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  1460. {
  1461. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1462. int page_count, i;
  1463. struct address_space *mapping;
  1464. struct sg_table *st;
  1465. struct scatterlist *sg;
  1466. struct sg_page_iter sg_iter;
  1467. struct page *page;
  1468. unsigned long last_pfn = 0; /* suppress gcc warning */
  1469. gfp_t gfp;
  1470. /* Assert that the object is not currently in any GPU domain. As it
  1471. * wasn't in the GTT, there shouldn't be any way it could have been in
  1472. * a GPU cache
  1473. */
  1474. BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  1475. BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  1476. st = kmalloc(sizeof(*st), GFP_KERNEL);
  1477. if (st == NULL)
  1478. return -ENOMEM;
  1479. page_count = obj->base.size / PAGE_SIZE;
  1480. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  1481. sg_free_table(st);
  1482. kfree(st);
  1483. return -ENOMEM;
  1484. }
  1485. /* Get the list of pages out of our struct file. They'll be pinned
  1486. * at this point until we release them.
  1487. *
  1488. * Fail silently without starting the shrinker
  1489. */
  1490. mapping = file_inode(obj->base.filp)->i_mapping;
  1491. gfp = mapping_gfp_mask(mapping);
  1492. gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
  1493. gfp &= ~(__GFP_IO | __GFP_WAIT);
  1494. sg = st->sgl;
  1495. st->nents = 0;
  1496. for (i = 0; i < page_count; i++) {
  1497. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1498. if (IS_ERR(page)) {
  1499. i915_gem_purge(dev_priv, page_count);
  1500. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1501. }
  1502. if (IS_ERR(page)) {
  1503. /* We've tried hard to allocate the memory by reaping
  1504. * our own buffer, now let the real VM do its job and
  1505. * go down in flames if truly OOM.
  1506. */
  1507. gfp &= ~(__GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD);
  1508. gfp |= __GFP_IO | __GFP_WAIT;
  1509. i915_gem_shrink_all(dev_priv);
  1510. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1511. if (IS_ERR(page))
  1512. goto err_pages;
  1513. gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
  1514. gfp &= ~(__GFP_IO | __GFP_WAIT);
  1515. }
  1516. #ifdef CONFIG_SWIOTLB
  1517. if (swiotlb_nr_tbl()) {
  1518. st->nents++;
  1519. sg_set_page(sg, page, PAGE_SIZE, 0);
  1520. sg = sg_next(sg);
  1521. continue;
  1522. }
  1523. #endif
  1524. if (!i || page_to_pfn(page) != last_pfn + 1) {
  1525. if (i)
  1526. sg = sg_next(sg);
  1527. st->nents++;
  1528. sg_set_page(sg, page, PAGE_SIZE, 0);
  1529. } else {
  1530. sg->length += PAGE_SIZE;
  1531. }
  1532. last_pfn = page_to_pfn(page);
  1533. }
  1534. #ifdef CONFIG_SWIOTLB
  1535. if (!swiotlb_nr_tbl())
  1536. #endif
  1537. sg_mark_end(sg);
  1538. obj->pages = st;
  1539. if (i915_gem_object_needs_bit17_swizzle(obj))
  1540. i915_gem_object_do_bit_17_swizzle(obj);
  1541. return 0;
  1542. err_pages:
  1543. sg_mark_end(sg);
  1544. for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
  1545. page_cache_release(sg_page_iter_page(&sg_iter));
  1546. sg_free_table(st);
  1547. kfree(st);
  1548. return PTR_ERR(page);
  1549. }
  1550. /* Ensure that the associated pages are gathered from the backing storage
  1551. * and pinned into our object. i915_gem_object_get_pages() may be called
  1552. * multiple times before they are released by a single call to
  1553. * i915_gem_object_put_pages() - once the pages are no longer referenced
  1554. * either as a result of memory pressure (reaping pages under the shrinker)
  1555. * or as the object is itself released.
  1556. */
  1557. int
  1558. i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  1559. {
  1560. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1561. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1562. int ret;
  1563. if (obj->pages)
  1564. return 0;
  1565. if (obj->madv != I915_MADV_WILLNEED) {
  1566. DRM_ERROR("Attempting to obtain a purgeable object\n");
  1567. return -EINVAL;
  1568. }
  1569. BUG_ON(obj->pages_pin_count);
  1570. ret = ops->get_pages(obj);
  1571. if (ret)
  1572. return ret;
  1573. list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  1574. return 0;
  1575. }
  1576. void
  1577. i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
  1578. struct intel_ring_buffer *ring)
  1579. {
  1580. struct drm_device *dev = obj->base.dev;
  1581. struct drm_i915_private *dev_priv = dev->dev_private;
  1582. struct i915_address_space *vm = &dev_priv->gtt.base;
  1583. u32 seqno = intel_ring_get_seqno(ring);
  1584. BUG_ON(ring == NULL);
  1585. if (obj->ring != ring && obj->last_write_seqno) {
  1586. /* Keep the seqno relative to the current ring */
  1587. obj->last_write_seqno = seqno;
  1588. }
  1589. obj->ring = ring;
  1590. /* Add a reference if we're newly entering the active list. */
  1591. if (!obj->active) {
  1592. drm_gem_object_reference(&obj->base);
  1593. obj->active = 1;
  1594. }
  1595. /* Move from whatever list we were on to the tail of execution. */
  1596. list_move_tail(&obj->mm_list, &vm->active_list);
  1597. list_move_tail(&obj->ring_list, &ring->active_list);
  1598. obj->last_read_seqno = seqno;
  1599. if (obj->fenced_gpu_access) {
  1600. obj->last_fenced_seqno = seqno;
  1601. /* Bump MRU to take account of the delayed flush */
  1602. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  1603. struct drm_i915_fence_reg *reg;
  1604. reg = &dev_priv->fence_regs[obj->fence_reg];
  1605. list_move_tail(&reg->lru_list,
  1606. &dev_priv->mm.fence_list);
  1607. }
  1608. }
  1609. }
  1610. static void
  1611. i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
  1612. {
  1613. struct drm_device *dev = obj->base.dev;
  1614. struct drm_i915_private *dev_priv = dev->dev_private;
  1615. struct i915_address_space *vm = &dev_priv->gtt.base;
  1616. BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
  1617. BUG_ON(!obj->active);
  1618. list_move_tail(&obj->mm_list, &vm->inactive_list);
  1619. list_del_init(&obj->ring_list);
  1620. obj->ring = NULL;
  1621. obj->last_read_seqno = 0;
  1622. obj->last_write_seqno = 0;
  1623. obj->base.write_domain = 0;
  1624. obj->last_fenced_seqno = 0;
  1625. obj->fenced_gpu_access = false;
  1626. obj->active = 0;
  1627. drm_gem_object_unreference(&obj->base);
  1628. WARN_ON(i915_verify_lists(dev));
  1629. }
  1630. static int
  1631. i915_gem_init_seqno(struct drm_device *dev, u32 seqno)
  1632. {
  1633. struct drm_i915_private *dev_priv = dev->dev_private;
  1634. struct intel_ring_buffer *ring;
  1635. int ret, i, j;
  1636. /* Carefully retire all requests without writing to the rings */
  1637. for_each_ring(ring, dev_priv, i) {
  1638. ret = intel_ring_idle(ring);
  1639. if (ret)
  1640. return ret;
  1641. }
  1642. i915_gem_retire_requests(dev);
  1643. /* Finally reset hw state */
  1644. for_each_ring(ring, dev_priv, i) {
  1645. intel_ring_init_seqno(ring, seqno);
  1646. for (j = 0; j < ARRAY_SIZE(ring->sync_seqno); j++)
  1647. ring->sync_seqno[j] = 0;
  1648. }
  1649. return 0;
  1650. }
  1651. int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
  1652. {
  1653. struct drm_i915_private *dev_priv = dev->dev_private;
  1654. int ret;
  1655. if (seqno == 0)
  1656. return -EINVAL;
  1657. /* HWS page needs to be set less than what we
  1658. * will inject to ring
  1659. */
  1660. ret = i915_gem_init_seqno(dev, seqno - 1);
  1661. if (ret)
  1662. return ret;
  1663. /* Carefully set the last_seqno value so that wrap
  1664. * detection still works
  1665. */
  1666. dev_priv->next_seqno = seqno;
  1667. dev_priv->last_seqno = seqno - 1;
  1668. if (dev_priv->last_seqno == 0)
  1669. dev_priv->last_seqno--;
  1670. return 0;
  1671. }
  1672. int
  1673. i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
  1674. {
  1675. struct drm_i915_private *dev_priv = dev->dev_private;
  1676. /* reserve 0 for non-seqno */
  1677. if (dev_priv->next_seqno == 0) {
  1678. int ret = i915_gem_init_seqno(dev, 0);
  1679. if (ret)
  1680. return ret;
  1681. dev_priv->next_seqno = 1;
  1682. }
  1683. *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
  1684. return 0;
  1685. }
  1686. int __i915_add_request(struct intel_ring_buffer *ring,
  1687. struct drm_file *file,
  1688. struct drm_i915_gem_object *obj,
  1689. u32 *out_seqno)
  1690. {
  1691. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  1692. struct drm_i915_gem_request *request;
  1693. u32 request_ring_position, request_start;
  1694. int was_empty;
  1695. int ret;
  1696. request_start = intel_ring_get_tail(ring);
  1697. /*
  1698. * Emit any outstanding flushes - execbuf can fail to emit the flush
  1699. * after having emitted the batchbuffer command. Hence we need to fix
  1700. * things up similar to emitting the lazy request. The difference here
  1701. * is that the flush _must_ happen before the next request, no matter
  1702. * what.
  1703. */
  1704. ret = intel_ring_flush_all_caches(ring);
  1705. if (ret)
  1706. return ret;
  1707. request = kmalloc(sizeof(*request), GFP_KERNEL);
  1708. if (request == NULL)
  1709. return -ENOMEM;
  1710. /* Record the position of the start of the request so that
  1711. * should we detect the updated seqno part-way through the
  1712. * GPU processing the request, we never over-estimate the
  1713. * position of the head.
  1714. */
  1715. request_ring_position = intel_ring_get_tail(ring);
  1716. ret = ring->add_request(ring);
  1717. if (ret) {
  1718. kfree(request);
  1719. return ret;
  1720. }
  1721. request->seqno = intel_ring_get_seqno(ring);
  1722. request->ring = ring;
  1723. request->head = request_start;
  1724. request->tail = request_ring_position;
  1725. request->ctx = ring->last_context;
  1726. request->batch_obj = obj;
  1727. /* Whilst this request exists, batch_obj will be on the
  1728. * active_list, and so will hold the active reference. Only when this
  1729. * request is retired will the the batch_obj be moved onto the
  1730. * inactive_list and lose its active reference. Hence we do not need
  1731. * to explicitly hold another reference here.
  1732. */
  1733. if (request->ctx)
  1734. i915_gem_context_reference(request->ctx);
  1735. request->emitted_jiffies = jiffies;
  1736. was_empty = list_empty(&ring->request_list);
  1737. list_add_tail(&request->list, &ring->request_list);
  1738. request->file_priv = NULL;
  1739. if (file) {
  1740. struct drm_i915_file_private *file_priv = file->driver_priv;
  1741. spin_lock(&file_priv->mm.lock);
  1742. request->file_priv = file_priv;
  1743. list_add_tail(&request->client_list,
  1744. &file_priv->mm.request_list);
  1745. spin_unlock(&file_priv->mm.lock);
  1746. }
  1747. trace_i915_gem_request_add(ring, request->seqno);
  1748. ring->outstanding_lazy_request = 0;
  1749. if (!dev_priv->ums.mm_suspended) {
  1750. i915_queue_hangcheck(ring->dev);
  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. /*
  1887. * Commit delayed tiling changes if we have an object still
  1888. * attached to the fence, otherwise just clear the fence.
  1889. */
  1890. if (reg->obj) {
  1891. i915_gem_object_update_fence(reg->obj, reg,
  1892. reg->obj->tiling_mode);
  1893. } else {
  1894. i915_gem_write_fence(dev, i, NULL);
  1895. }
  1896. }
  1897. }
  1898. void i915_gem_reset(struct drm_device *dev)
  1899. {
  1900. struct drm_i915_private *dev_priv = dev->dev_private;
  1901. struct i915_address_space *vm = &dev_priv->gtt.base;
  1902. struct drm_i915_gem_object *obj;
  1903. struct intel_ring_buffer *ring;
  1904. int i;
  1905. for_each_ring(ring, dev_priv, i)
  1906. i915_gem_reset_ring_lists(dev_priv, ring);
  1907. /* Move everything out of the GPU domains to ensure we do any
  1908. * necessary invalidation upon reuse.
  1909. */
  1910. list_for_each_entry(obj, &vm->inactive_list, mm_list)
  1911. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  1912. i915_gem_restore_fences(dev);
  1913. }
  1914. /**
  1915. * This function clears the request list as sequence numbers are passed.
  1916. */
  1917. void
  1918. i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
  1919. {
  1920. uint32_t seqno;
  1921. if (list_empty(&ring->request_list))
  1922. return;
  1923. WARN_ON(i915_verify_lists(ring->dev));
  1924. seqno = ring->get_seqno(ring, true);
  1925. while (!list_empty(&ring->request_list)) {
  1926. struct drm_i915_gem_request *request;
  1927. request = list_first_entry(&ring->request_list,
  1928. struct drm_i915_gem_request,
  1929. list);
  1930. if (!i915_seqno_passed(seqno, request->seqno))
  1931. break;
  1932. trace_i915_gem_request_retire(ring, request->seqno);
  1933. /* We know the GPU must have read the request to have
  1934. * sent us the seqno + interrupt, so use the position
  1935. * of tail of the request to update the last known position
  1936. * of the GPU head.
  1937. */
  1938. ring->last_retired_head = request->tail;
  1939. i915_gem_free_request(request);
  1940. }
  1941. /* Move any buffers on the active list that are no longer referenced
  1942. * by the ringbuffer to the flushing/inactive lists as appropriate.
  1943. */
  1944. while (!list_empty(&ring->active_list)) {
  1945. struct drm_i915_gem_object *obj;
  1946. obj = list_first_entry(&ring->active_list,
  1947. struct drm_i915_gem_object,
  1948. ring_list);
  1949. if (!i915_seqno_passed(seqno, obj->last_read_seqno))
  1950. break;
  1951. i915_gem_object_move_to_inactive(obj);
  1952. }
  1953. if (unlikely(ring->trace_irq_seqno &&
  1954. i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
  1955. ring->irq_put(ring);
  1956. ring->trace_irq_seqno = 0;
  1957. }
  1958. WARN_ON(i915_verify_lists(ring->dev));
  1959. }
  1960. void
  1961. i915_gem_retire_requests(struct drm_device *dev)
  1962. {
  1963. drm_i915_private_t *dev_priv = dev->dev_private;
  1964. struct intel_ring_buffer *ring;
  1965. int i;
  1966. for_each_ring(ring, dev_priv, i)
  1967. i915_gem_retire_requests_ring(ring);
  1968. }
  1969. static void
  1970. i915_gem_retire_work_handler(struct work_struct *work)
  1971. {
  1972. drm_i915_private_t *dev_priv;
  1973. struct drm_device *dev;
  1974. struct intel_ring_buffer *ring;
  1975. bool idle;
  1976. int i;
  1977. dev_priv = container_of(work, drm_i915_private_t,
  1978. mm.retire_work.work);
  1979. dev = dev_priv->dev;
  1980. /* Come back later if the device is busy... */
  1981. if (!mutex_trylock(&dev->struct_mutex)) {
  1982. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  1983. round_jiffies_up_relative(HZ));
  1984. return;
  1985. }
  1986. i915_gem_retire_requests(dev);
  1987. /* Send a periodic flush down the ring so we don't hold onto GEM
  1988. * objects indefinitely.
  1989. */
  1990. idle = true;
  1991. for_each_ring(ring, dev_priv, i) {
  1992. if (ring->gpu_caches_dirty)
  1993. i915_add_request(ring, NULL);
  1994. idle &= list_empty(&ring->request_list);
  1995. }
  1996. if (!dev_priv->ums.mm_suspended && !idle)
  1997. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  1998. round_jiffies_up_relative(HZ));
  1999. if (idle)
  2000. intel_mark_idle(dev);
  2001. mutex_unlock(&dev->struct_mutex);
  2002. }
  2003. /**
  2004. * Ensures that an object will eventually get non-busy by flushing any required
  2005. * write domains, emitting any outstanding lazy request and retiring and
  2006. * completed requests.
  2007. */
  2008. static int
  2009. i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
  2010. {
  2011. int ret;
  2012. if (obj->active) {
  2013. ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
  2014. if (ret)
  2015. return ret;
  2016. i915_gem_retire_requests_ring(obj->ring);
  2017. }
  2018. return 0;
  2019. }
  2020. /**
  2021. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2022. * @DRM_IOCTL_ARGS: standard ioctl arguments
  2023. *
  2024. * Returns 0 if successful, else an error is returned with the remaining time in
  2025. * the timeout parameter.
  2026. * -ETIME: object is still busy after timeout
  2027. * -ERESTARTSYS: signal interrupted the wait
  2028. * -ENONENT: object doesn't exist
  2029. * Also possible, but rare:
  2030. * -EAGAIN: GPU wedged
  2031. * -ENOMEM: damn
  2032. * -ENODEV: Internal IRQ fail
  2033. * -E?: The add request failed
  2034. *
  2035. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2036. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2037. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2038. * without holding struct_mutex the object may become re-busied before this
  2039. * function completes. A similar but shorter * race condition exists in the busy
  2040. * ioctl
  2041. */
  2042. int
  2043. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2044. {
  2045. drm_i915_private_t *dev_priv = dev->dev_private;
  2046. struct drm_i915_gem_wait *args = data;
  2047. struct drm_i915_gem_object *obj;
  2048. struct intel_ring_buffer *ring = NULL;
  2049. struct timespec timeout_stack, *timeout = NULL;
  2050. unsigned reset_counter;
  2051. u32 seqno = 0;
  2052. int ret = 0;
  2053. if (args->timeout_ns >= 0) {
  2054. timeout_stack = ns_to_timespec(args->timeout_ns);
  2055. timeout = &timeout_stack;
  2056. }
  2057. ret = i915_mutex_lock_interruptible(dev);
  2058. if (ret)
  2059. return ret;
  2060. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
  2061. if (&obj->base == NULL) {
  2062. mutex_unlock(&dev->struct_mutex);
  2063. return -ENOENT;
  2064. }
  2065. /* Need to make sure the object gets inactive eventually. */
  2066. ret = i915_gem_object_flush_active(obj);
  2067. if (ret)
  2068. goto out;
  2069. if (obj->active) {
  2070. seqno = obj->last_read_seqno;
  2071. ring = obj->ring;
  2072. }
  2073. if (seqno == 0)
  2074. goto out;
  2075. /* Do this after OLR check to make sure we make forward progress polling
  2076. * on this IOCTL with a 0 timeout (like busy ioctl)
  2077. */
  2078. if (!args->timeout_ns) {
  2079. ret = -ETIME;
  2080. goto out;
  2081. }
  2082. drm_gem_object_unreference(&obj->base);
  2083. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  2084. mutex_unlock(&dev->struct_mutex);
  2085. ret = __wait_seqno(ring, seqno, reset_counter, true, timeout);
  2086. if (timeout)
  2087. args->timeout_ns = timespec_to_ns(timeout);
  2088. return ret;
  2089. out:
  2090. drm_gem_object_unreference(&obj->base);
  2091. mutex_unlock(&dev->struct_mutex);
  2092. return ret;
  2093. }
  2094. /**
  2095. * i915_gem_object_sync - sync an object to a ring.
  2096. *
  2097. * @obj: object which may be in use on another ring.
  2098. * @to: ring we wish to use the object on. May be NULL.
  2099. *
  2100. * This code is meant to abstract object synchronization with the GPU.
  2101. * Calling with NULL implies synchronizing the object with the CPU
  2102. * rather than a particular GPU ring.
  2103. *
  2104. * Returns 0 if successful, else propagates up the lower layer error.
  2105. */
  2106. int
  2107. i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2108. struct intel_ring_buffer *to)
  2109. {
  2110. struct intel_ring_buffer *from = obj->ring;
  2111. u32 seqno;
  2112. int ret, idx;
  2113. if (from == NULL || to == from)
  2114. return 0;
  2115. if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
  2116. return i915_gem_object_wait_rendering(obj, false);
  2117. idx = intel_ring_sync_index(from, to);
  2118. seqno = obj->last_read_seqno;
  2119. if (seqno <= from->sync_seqno[idx])
  2120. return 0;
  2121. ret = i915_gem_check_olr(obj->ring, seqno);
  2122. if (ret)
  2123. return ret;
  2124. ret = to->sync_to(to, from, seqno);
  2125. if (!ret)
  2126. /* We use last_read_seqno because sync_to()
  2127. * might have just caused seqno wrap under
  2128. * the radar.
  2129. */
  2130. from->sync_seqno[idx] = obj->last_read_seqno;
  2131. return ret;
  2132. }
  2133. static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
  2134. {
  2135. u32 old_write_domain, old_read_domains;
  2136. /* Force a pagefault for domain tracking on next user access */
  2137. i915_gem_release_mmap(obj);
  2138. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2139. return;
  2140. /* Wait for any direct GTT access to complete */
  2141. mb();
  2142. old_read_domains = obj->base.read_domains;
  2143. old_write_domain = obj->base.write_domain;
  2144. obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
  2145. obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
  2146. trace_i915_gem_object_change_domain(obj,
  2147. old_read_domains,
  2148. old_write_domain);
  2149. }
  2150. /**
  2151. * Unbinds an object from the GTT aperture.
  2152. */
  2153. int
  2154. i915_gem_object_unbind(struct drm_i915_gem_object *obj)
  2155. {
  2156. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  2157. struct i915_vma *vma;
  2158. int ret;
  2159. if (!i915_gem_obj_ggtt_bound(obj))
  2160. return 0;
  2161. if (obj->pin_count)
  2162. return -EBUSY;
  2163. BUG_ON(obj->pages == NULL);
  2164. ret = i915_gem_object_finish_gpu(obj);
  2165. if (ret)
  2166. return ret;
  2167. /* Continue on if we fail due to EIO, the GPU is hung so we
  2168. * should be safe and we need to cleanup or else we might
  2169. * cause memory corruption through use-after-free.
  2170. */
  2171. i915_gem_object_finish_gtt(obj);
  2172. /* release the fence reg _after_ flushing */
  2173. ret = i915_gem_object_put_fence(obj);
  2174. if (ret)
  2175. return ret;
  2176. trace_i915_gem_object_unbind(obj);
  2177. if (obj->has_global_gtt_mapping)
  2178. i915_gem_gtt_unbind_object(obj);
  2179. if (obj->has_aliasing_ppgtt_mapping) {
  2180. i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
  2181. obj->has_aliasing_ppgtt_mapping = 0;
  2182. }
  2183. i915_gem_gtt_finish_object(obj);
  2184. i915_gem_object_unpin_pages(obj);
  2185. list_del(&obj->mm_list);
  2186. /* Avoid an unnecessary call to unbind on rebind. */
  2187. obj->map_and_fenceable = true;
  2188. vma = __i915_gem_obj_to_vma(obj);
  2189. list_del(&vma->vma_link);
  2190. drm_mm_remove_node(&vma->node);
  2191. i915_gem_vma_destroy(vma);
  2192. /* Since the unbound list is global, only move to that list if
  2193. * no more VMAs exist.
  2194. * NB: Until we have real VMAs there will only ever be one */
  2195. WARN_ON(!list_empty(&obj->vma_list));
  2196. if (list_empty(&obj->vma_list))
  2197. list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  2198. return 0;
  2199. }
  2200. int i915_gpu_idle(struct drm_device *dev)
  2201. {
  2202. drm_i915_private_t *dev_priv = dev->dev_private;
  2203. struct intel_ring_buffer *ring;
  2204. int ret, i;
  2205. /* Flush everything onto the inactive list. */
  2206. for_each_ring(ring, dev_priv, i) {
  2207. ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID);
  2208. if (ret)
  2209. return ret;
  2210. ret = intel_ring_idle(ring);
  2211. if (ret)
  2212. return ret;
  2213. }
  2214. return 0;
  2215. }
  2216. static void i965_write_fence_reg(struct drm_device *dev, int reg,
  2217. struct drm_i915_gem_object *obj)
  2218. {
  2219. drm_i915_private_t *dev_priv = dev->dev_private;
  2220. int fence_reg;
  2221. int fence_pitch_shift;
  2222. if (INTEL_INFO(dev)->gen >= 6) {
  2223. fence_reg = FENCE_REG_SANDYBRIDGE_0;
  2224. fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT;
  2225. } else {
  2226. fence_reg = FENCE_REG_965_0;
  2227. fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
  2228. }
  2229. fence_reg += reg * 8;
  2230. /* To w/a incoherency with non-atomic 64-bit register updates,
  2231. * we split the 64-bit update into two 32-bit writes. In order
  2232. * for a partial fence not to be evaluated between writes, we
  2233. * precede the update with write to turn off the fence register,
  2234. * and only enable the fence as the last step.
  2235. *
  2236. * For extra levels of paranoia, we make sure each step lands
  2237. * before applying the next step.
  2238. */
  2239. I915_WRITE(fence_reg, 0);
  2240. POSTING_READ(fence_reg);
  2241. if (obj) {
  2242. u32 size = i915_gem_obj_ggtt_size(obj);
  2243. uint64_t val;
  2244. val = (uint64_t)((i915_gem_obj_ggtt_offset(obj) + size - 4096) &
  2245. 0xfffff000) << 32;
  2246. val |= i915_gem_obj_ggtt_offset(obj) & 0xfffff000;
  2247. val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
  2248. if (obj->tiling_mode == I915_TILING_Y)
  2249. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  2250. val |= I965_FENCE_REG_VALID;
  2251. I915_WRITE(fence_reg + 4, val >> 32);
  2252. POSTING_READ(fence_reg + 4);
  2253. I915_WRITE(fence_reg + 0, val);
  2254. POSTING_READ(fence_reg);
  2255. } else {
  2256. I915_WRITE(fence_reg + 4, 0);
  2257. POSTING_READ(fence_reg + 4);
  2258. }
  2259. }
  2260. static void i915_write_fence_reg(struct drm_device *dev, int reg,
  2261. struct drm_i915_gem_object *obj)
  2262. {
  2263. drm_i915_private_t *dev_priv = dev->dev_private;
  2264. u32 val;
  2265. if (obj) {
  2266. u32 size = i915_gem_obj_ggtt_size(obj);
  2267. int pitch_val;
  2268. int tile_width;
  2269. WARN((i915_gem_obj_ggtt_offset(obj) & ~I915_FENCE_START_MASK) ||
  2270. (size & -size) != size ||
  2271. (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
  2272. "object 0x%08lx [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
  2273. i915_gem_obj_ggtt_offset(obj), obj->map_and_fenceable, size);
  2274. if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
  2275. tile_width = 128;
  2276. else
  2277. tile_width = 512;
  2278. /* Note: pitch better be a power of two tile widths */
  2279. pitch_val = obj->stride / tile_width;
  2280. pitch_val = ffs(pitch_val) - 1;
  2281. val = i915_gem_obj_ggtt_offset(obj);
  2282. if (obj->tiling_mode == I915_TILING_Y)
  2283. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2284. val |= I915_FENCE_SIZE_BITS(size);
  2285. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2286. val |= I830_FENCE_REG_VALID;
  2287. } else
  2288. val = 0;
  2289. if (reg < 8)
  2290. reg = FENCE_REG_830_0 + reg * 4;
  2291. else
  2292. reg = FENCE_REG_945_8 + (reg - 8) * 4;
  2293. I915_WRITE(reg, val);
  2294. POSTING_READ(reg);
  2295. }
  2296. static void i830_write_fence_reg(struct drm_device *dev, int reg,
  2297. struct drm_i915_gem_object *obj)
  2298. {
  2299. drm_i915_private_t *dev_priv = dev->dev_private;
  2300. uint32_t val;
  2301. if (obj) {
  2302. u32 size = i915_gem_obj_ggtt_size(obj);
  2303. uint32_t pitch_val;
  2304. WARN((i915_gem_obj_ggtt_offset(obj) & ~I830_FENCE_START_MASK) ||
  2305. (size & -size) != size ||
  2306. (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
  2307. "object 0x%08lx not 512K or pot-size 0x%08x aligned\n",
  2308. i915_gem_obj_ggtt_offset(obj), size);
  2309. pitch_val = obj->stride / 128;
  2310. pitch_val = ffs(pitch_val) - 1;
  2311. val = i915_gem_obj_ggtt_offset(obj);
  2312. if (obj->tiling_mode == I915_TILING_Y)
  2313. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2314. val |= I830_FENCE_SIZE_BITS(size);
  2315. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2316. val |= I830_FENCE_REG_VALID;
  2317. } else
  2318. val = 0;
  2319. I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
  2320. POSTING_READ(FENCE_REG_830_0 + reg * 4);
  2321. }
  2322. inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
  2323. {
  2324. return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
  2325. }
  2326. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  2327. struct drm_i915_gem_object *obj)
  2328. {
  2329. struct drm_i915_private *dev_priv = dev->dev_private;
  2330. /* Ensure that all CPU reads are completed before installing a fence
  2331. * and all writes before removing the fence.
  2332. */
  2333. if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
  2334. mb();
  2335. WARN(obj && (!obj->stride || !obj->tiling_mode),
  2336. "bogus fence setup with stride: 0x%x, tiling mode: %i\n",
  2337. obj->stride, obj->tiling_mode);
  2338. switch (INTEL_INFO(dev)->gen) {
  2339. case 7:
  2340. case 6:
  2341. case 5:
  2342. case 4: i965_write_fence_reg(dev, reg, obj); break;
  2343. case 3: i915_write_fence_reg(dev, reg, obj); break;
  2344. case 2: i830_write_fence_reg(dev, reg, obj); break;
  2345. default: BUG();
  2346. }
  2347. /* And similarly be paranoid that no direct access to this region
  2348. * is reordered to before the fence is installed.
  2349. */
  2350. if (i915_gem_object_needs_mb(obj))
  2351. mb();
  2352. }
  2353. static inline int fence_number(struct drm_i915_private *dev_priv,
  2354. struct drm_i915_fence_reg *fence)
  2355. {
  2356. return fence - dev_priv->fence_regs;
  2357. }
  2358. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  2359. struct drm_i915_fence_reg *fence,
  2360. bool enable)
  2361. {
  2362. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2363. int reg = fence_number(dev_priv, fence);
  2364. i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
  2365. if (enable) {
  2366. obj->fence_reg = reg;
  2367. fence->obj = obj;
  2368. list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
  2369. } else {
  2370. obj->fence_reg = I915_FENCE_REG_NONE;
  2371. fence->obj = NULL;
  2372. list_del_init(&fence->lru_list);
  2373. }
  2374. obj->fence_dirty = false;
  2375. }
  2376. static int
  2377. i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
  2378. {
  2379. if (obj->last_fenced_seqno) {
  2380. int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
  2381. if (ret)
  2382. return ret;
  2383. obj->last_fenced_seqno = 0;
  2384. }
  2385. obj->fenced_gpu_access = false;
  2386. return 0;
  2387. }
  2388. int
  2389. i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
  2390. {
  2391. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2392. struct drm_i915_fence_reg *fence;
  2393. int ret;
  2394. ret = i915_gem_object_wait_fence(obj);
  2395. if (ret)
  2396. return ret;
  2397. if (obj->fence_reg == I915_FENCE_REG_NONE)
  2398. return 0;
  2399. fence = &dev_priv->fence_regs[obj->fence_reg];
  2400. i915_gem_object_fence_lost(obj);
  2401. i915_gem_object_update_fence(obj, fence, false);
  2402. return 0;
  2403. }
  2404. static struct drm_i915_fence_reg *
  2405. i915_find_fence_reg(struct drm_device *dev)
  2406. {
  2407. struct drm_i915_private *dev_priv = dev->dev_private;
  2408. struct drm_i915_fence_reg *reg, *avail;
  2409. int i;
  2410. /* First try to find a free reg */
  2411. avail = NULL;
  2412. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  2413. reg = &dev_priv->fence_regs[i];
  2414. if (!reg->obj)
  2415. return reg;
  2416. if (!reg->pin_count)
  2417. avail = reg;
  2418. }
  2419. if (avail == NULL)
  2420. return NULL;
  2421. /* None available, try to steal one or wait for a user to finish */
  2422. list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
  2423. if (reg->pin_count)
  2424. continue;
  2425. return reg;
  2426. }
  2427. return NULL;
  2428. }
  2429. /**
  2430. * i915_gem_object_get_fence - set up fencing for an object
  2431. * @obj: object to map through a fence reg
  2432. *
  2433. * When mapping objects through the GTT, userspace wants to be able to write
  2434. * to them without having to worry about swizzling if the object is tiled.
  2435. * This function walks the fence regs looking for a free one for @obj,
  2436. * stealing one if it can't find any.
  2437. *
  2438. * It then sets up the reg based on the object's properties: address, pitch
  2439. * and tiling format.
  2440. *
  2441. * For an untiled surface, this removes any existing fence.
  2442. */
  2443. int
  2444. i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
  2445. {
  2446. struct drm_device *dev = obj->base.dev;
  2447. struct drm_i915_private *dev_priv = dev->dev_private;
  2448. bool enable = obj->tiling_mode != I915_TILING_NONE;
  2449. struct drm_i915_fence_reg *reg;
  2450. int ret;
  2451. /* Have we updated the tiling parameters upon the object and so
  2452. * will need to serialise the write to the associated fence register?
  2453. */
  2454. if (obj->fence_dirty) {
  2455. ret = i915_gem_object_wait_fence(obj);
  2456. if (ret)
  2457. return ret;
  2458. }
  2459. /* Just update our place in the LRU if our fence is getting reused. */
  2460. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  2461. reg = &dev_priv->fence_regs[obj->fence_reg];
  2462. if (!obj->fence_dirty) {
  2463. list_move_tail(&reg->lru_list,
  2464. &dev_priv->mm.fence_list);
  2465. return 0;
  2466. }
  2467. } else if (enable) {
  2468. reg = i915_find_fence_reg(dev);
  2469. if (reg == NULL)
  2470. return -EDEADLK;
  2471. if (reg->obj) {
  2472. struct drm_i915_gem_object *old = reg->obj;
  2473. ret = i915_gem_object_wait_fence(old);
  2474. if (ret)
  2475. return ret;
  2476. i915_gem_object_fence_lost(old);
  2477. }
  2478. } else
  2479. return 0;
  2480. i915_gem_object_update_fence(obj, reg, enable);
  2481. return 0;
  2482. }
  2483. static bool i915_gem_valid_gtt_space(struct drm_device *dev,
  2484. struct drm_mm_node *gtt_space,
  2485. unsigned long cache_level)
  2486. {
  2487. struct drm_mm_node *other;
  2488. /* On non-LLC machines we have to be careful when putting differing
  2489. * types of snoopable memory together to avoid the prefetcher
  2490. * crossing memory domains and dying.
  2491. */
  2492. if (HAS_LLC(dev))
  2493. return true;
  2494. if (!drm_mm_node_allocated(gtt_space))
  2495. return true;
  2496. if (list_empty(&gtt_space->node_list))
  2497. return true;
  2498. other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
  2499. if (other->allocated && !other->hole_follows && other->color != cache_level)
  2500. return false;
  2501. other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
  2502. if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
  2503. return false;
  2504. return true;
  2505. }
  2506. static void i915_gem_verify_gtt(struct drm_device *dev)
  2507. {
  2508. #if WATCH_GTT
  2509. struct drm_i915_private *dev_priv = dev->dev_private;
  2510. struct drm_i915_gem_object *obj;
  2511. int err = 0;
  2512. list_for_each_entry(obj, &dev_priv->mm.gtt_list, global_list) {
  2513. if (obj->gtt_space == NULL) {
  2514. printk(KERN_ERR "object found on GTT list with no space reserved\n");
  2515. err++;
  2516. continue;
  2517. }
  2518. if (obj->cache_level != obj->gtt_space->color) {
  2519. printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n",
  2520. i915_gem_obj_ggtt_offset(obj),
  2521. i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj),
  2522. obj->cache_level,
  2523. obj->gtt_space->color);
  2524. err++;
  2525. continue;
  2526. }
  2527. if (!i915_gem_valid_gtt_space(dev,
  2528. obj->gtt_space,
  2529. obj->cache_level)) {
  2530. printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n",
  2531. i915_gem_obj_ggtt_offset(obj),
  2532. i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj),
  2533. obj->cache_level);
  2534. err++;
  2535. continue;
  2536. }
  2537. }
  2538. WARN_ON(err);
  2539. #endif
  2540. }
  2541. /**
  2542. * Finds free space in the GTT aperture and binds the object there.
  2543. */
  2544. static int
  2545. i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
  2546. unsigned alignment,
  2547. bool map_and_fenceable,
  2548. bool nonblocking)
  2549. {
  2550. struct drm_device *dev = obj->base.dev;
  2551. drm_i915_private_t *dev_priv = dev->dev_private;
  2552. struct i915_address_space *vm = &dev_priv->gtt.base;
  2553. u32 size, fence_size, fence_alignment, unfenced_alignment;
  2554. bool mappable, fenceable;
  2555. size_t gtt_max = map_and_fenceable ?
  2556. dev_priv->gtt.mappable_end : dev_priv->gtt.base.total;
  2557. struct i915_vma *vma;
  2558. int ret;
  2559. if (WARN_ON(!list_empty(&obj->vma_list)))
  2560. return -EBUSY;
  2561. fence_size = i915_gem_get_gtt_size(dev,
  2562. obj->base.size,
  2563. obj->tiling_mode);
  2564. fence_alignment = i915_gem_get_gtt_alignment(dev,
  2565. obj->base.size,
  2566. obj->tiling_mode, true);
  2567. unfenced_alignment =
  2568. i915_gem_get_gtt_alignment(dev,
  2569. obj->base.size,
  2570. obj->tiling_mode, false);
  2571. if (alignment == 0)
  2572. alignment = map_and_fenceable ? fence_alignment :
  2573. unfenced_alignment;
  2574. if (map_and_fenceable && alignment & (fence_alignment - 1)) {
  2575. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  2576. return -EINVAL;
  2577. }
  2578. size = map_and_fenceable ? fence_size : obj->base.size;
  2579. /* If the object is bigger than the entire aperture, reject it early
  2580. * before evicting everything in a vain attempt to find space.
  2581. */
  2582. if (obj->base.size > gtt_max) {
  2583. DRM_ERROR("Attempting to bind an object larger than the aperture: object=%zd > %s aperture=%zu\n",
  2584. obj->base.size,
  2585. map_and_fenceable ? "mappable" : "total",
  2586. gtt_max);
  2587. return -E2BIG;
  2588. }
  2589. ret = i915_gem_object_get_pages(obj);
  2590. if (ret)
  2591. return ret;
  2592. i915_gem_object_pin_pages(obj);
  2593. vma = i915_gem_vma_create(obj, &dev_priv->gtt.base);
  2594. if (IS_ERR(vma)) {
  2595. ret = PTR_ERR(vma);
  2596. goto err_unpin;
  2597. }
  2598. search_free:
  2599. ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
  2600. &vma->node,
  2601. size, alignment,
  2602. obj->cache_level, 0, gtt_max);
  2603. if (ret) {
  2604. ret = i915_gem_evict_something(dev, size, alignment,
  2605. obj->cache_level,
  2606. map_and_fenceable,
  2607. nonblocking);
  2608. if (ret == 0)
  2609. goto search_free;
  2610. goto err_free_vma;
  2611. }
  2612. if (WARN_ON(!i915_gem_valid_gtt_space(dev, &vma->node,
  2613. obj->cache_level))) {
  2614. ret = -EINVAL;
  2615. goto err_remove_node;
  2616. }
  2617. ret = i915_gem_gtt_prepare_object(obj);
  2618. if (ret)
  2619. goto err_remove_node;
  2620. list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
  2621. list_add_tail(&obj->mm_list, &vm->inactive_list);
  2622. list_add(&vma->vma_link, &obj->vma_list);
  2623. fenceable =
  2624. i915_gem_obj_ggtt_size(obj) == fence_size &&
  2625. (i915_gem_obj_ggtt_offset(obj) & (fence_alignment - 1)) == 0;
  2626. mappable = i915_gem_obj_ggtt_offset(obj) + obj->base.size <=
  2627. dev_priv->gtt.mappable_end;
  2628. obj->map_and_fenceable = mappable && fenceable;
  2629. trace_i915_gem_object_bind(obj, map_and_fenceable);
  2630. i915_gem_verify_gtt(dev);
  2631. return 0;
  2632. err_remove_node:
  2633. drm_mm_remove_node(&vma->node);
  2634. err_free_vma:
  2635. i915_gem_vma_destroy(vma);
  2636. err_unpin:
  2637. i915_gem_object_unpin_pages(obj);
  2638. return ret;
  2639. }
  2640. void
  2641. i915_gem_clflush_object(struct drm_i915_gem_object *obj)
  2642. {
  2643. /* If we don't have a page list set up, then we're not pinned
  2644. * to GPU, and we can ignore the cache flush because it'll happen
  2645. * again at bind time.
  2646. */
  2647. if (obj->pages == NULL)
  2648. return;
  2649. /*
  2650. * Stolen memory is always coherent with the GPU as it is explicitly
  2651. * marked as wc by the system, or the system is cache-coherent.
  2652. */
  2653. if (obj->stolen)
  2654. return;
  2655. /* If the GPU is snooping the contents of the CPU cache,
  2656. * we do not need to manually clear the CPU cache lines. However,
  2657. * the caches are only snooped when the render cache is
  2658. * flushed/invalidated. As we always have to emit invalidations
  2659. * and flushes when moving into and out of the RENDER domain, correct
  2660. * snooping behaviour occurs naturally as the result of our domain
  2661. * tracking.
  2662. */
  2663. if (obj->cache_level != I915_CACHE_NONE)
  2664. return;
  2665. trace_i915_gem_object_clflush(obj);
  2666. drm_clflush_sg(obj->pages);
  2667. }
  2668. /** Flushes the GTT write domain for the object if it's dirty. */
  2669. static void
  2670. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  2671. {
  2672. uint32_t old_write_domain;
  2673. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  2674. return;
  2675. /* No actual flushing is required for the GTT write domain. Writes
  2676. * to it immediately go to main memory as far as we know, so there's
  2677. * no chipset flush. It also doesn't land in render cache.
  2678. *
  2679. * However, we do have to enforce the order so that all writes through
  2680. * the GTT land before any writes to the device, such as updates to
  2681. * the GATT itself.
  2682. */
  2683. wmb();
  2684. old_write_domain = obj->base.write_domain;
  2685. obj->base.write_domain = 0;
  2686. trace_i915_gem_object_change_domain(obj,
  2687. obj->base.read_domains,
  2688. old_write_domain);
  2689. }
  2690. /** Flushes the CPU write domain for the object if it's dirty. */
  2691. static void
  2692. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  2693. {
  2694. uint32_t old_write_domain;
  2695. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  2696. return;
  2697. i915_gem_clflush_object(obj);
  2698. i915_gem_chipset_flush(obj->base.dev);
  2699. old_write_domain = obj->base.write_domain;
  2700. obj->base.write_domain = 0;
  2701. trace_i915_gem_object_change_domain(obj,
  2702. obj->base.read_domains,
  2703. old_write_domain);
  2704. }
  2705. /**
  2706. * Moves a single object to the GTT read, and possibly write domain.
  2707. *
  2708. * This function returns when the move is complete, including waiting on
  2709. * flushes to occur.
  2710. */
  2711. int
  2712. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  2713. {
  2714. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  2715. uint32_t old_write_domain, old_read_domains;
  2716. int ret;
  2717. /* Not valid to be called on unbound objects. */
  2718. if (!i915_gem_obj_ggtt_bound(obj))
  2719. return -EINVAL;
  2720. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  2721. return 0;
  2722. ret = i915_gem_object_wait_rendering(obj, !write);
  2723. if (ret)
  2724. return ret;
  2725. i915_gem_object_flush_cpu_write_domain(obj);
  2726. /* Serialise direct access to this object with the barriers for
  2727. * coherent writes from the GPU, by effectively invalidating the
  2728. * GTT domain upon first access.
  2729. */
  2730. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2731. mb();
  2732. old_write_domain = obj->base.write_domain;
  2733. old_read_domains = obj->base.read_domains;
  2734. /* It should now be out of any other write domains, and we can update
  2735. * the domain values for our changes.
  2736. */
  2737. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2738. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2739. if (write) {
  2740. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  2741. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  2742. obj->dirty = 1;
  2743. }
  2744. trace_i915_gem_object_change_domain(obj,
  2745. old_read_domains,
  2746. old_write_domain);
  2747. /* And bump the LRU for this access */
  2748. if (i915_gem_object_is_inactive(obj))
  2749. list_move_tail(&obj->mm_list,
  2750. &dev_priv->gtt.base.inactive_list);
  2751. return 0;
  2752. }
  2753. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  2754. enum i915_cache_level cache_level)
  2755. {
  2756. struct drm_device *dev = obj->base.dev;
  2757. drm_i915_private_t *dev_priv = dev->dev_private;
  2758. struct i915_vma *vma = __i915_gem_obj_to_vma(obj);
  2759. int ret;
  2760. if (obj->cache_level == cache_level)
  2761. return 0;
  2762. if (obj->pin_count) {
  2763. DRM_DEBUG("can not change the cache level of pinned objects\n");
  2764. return -EBUSY;
  2765. }
  2766. if (vma && !i915_gem_valid_gtt_space(dev, &vma->node, cache_level)) {
  2767. ret = i915_gem_object_unbind(obj);
  2768. if (ret)
  2769. return ret;
  2770. }
  2771. if (i915_gem_obj_ggtt_bound(obj)) {
  2772. ret = i915_gem_object_finish_gpu(obj);
  2773. if (ret)
  2774. return ret;
  2775. i915_gem_object_finish_gtt(obj);
  2776. /* Before SandyBridge, you could not use tiling or fence
  2777. * registers with snooped memory, so relinquish any fences
  2778. * currently pointing to our region in the aperture.
  2779. */
  2780. if (INTEL_INFO(dev)->gen < 6) {
  2781. ret = i915_gem_object_put_fence(obj);
  2782. if (ret)
  2783. return ret;
  2784. }
  2785. if (obj->has_global_gtt_mapping)
  2786. i915_gem_gtt_bind_object(obj, cache_level);
  2787. if (obj->has_aliasing_ppgtt_mapping)
  2788. i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
  2789. obj, cache_level);
  2790. i915_gem_obj_ggtt_set_color(obj, cache_level);
  2791. }
  2792. if (cache_level == I915_CACHE_NONE) {
  2793. u32 old_read_domains, old_write_domain;
  2794. /* If we're coming from LLC cached, then we haven't
  2795. * actually been tracking whether the data is in the
  2796. * CPU cache or not, since we only allow one bit set
  2797. * in obj->write_domain and have been skipping the clflushes.
  2798. * Just set it to the CPU cache for now.
  2799. */
  2800. WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
  2801. WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
  2802. old_read_domains = obj->base.read_domains;
  2803. old_write_domain = obj->base.write_domain;
  2804. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2805. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2806. trace_i915_gem_object_change_domain(obj,
  2807. old_read_domains,
  2808. old_write_domain);
  2809. }
  2810. obj->cache_level = cache_level;
  2811. i915_gem_verify_gtt(dev);
  2812. return 0;
  2813. }
  2814. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  2815. struct drm_file *file)
  2816. {
  2817. struct drm_i915_gem_caching *args = data;
  2818. struct drm_i915_gem_object *obj;
  2819. int ret;
  2820. ret = i915_mutex_lock_interruptible(dev);
  2821. if (ret)
  2822. return ret;
  2823. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2824. if (&obj->base == NULL) {
  2825. ret = -ENOENT;
  2826. goto unlock;
  2827. }
  2828. args->caching = obj->cache_level != I915_CACHE_NONE;
  2829. drm_gem_object_unreference(&obj->base);
  2830. unlock:
  2831. mutex_unlock(&dev->struct_mutex);
  2832. return ret;
  2833. }
  2834. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  2835. struct drm_file *file)
  2836. {
  2837. struct drm_i915_gem_caching *args = data;
  2838. struct drm_i915_gem_object *obj;
  2839. enum i915_cache_level level;
  2840. int ret;
  2841. switch (args->caching) {
  2842. case I915_CACHING_NONE:
  2843. level = I915_CACHE_NONE;
  2844. break;
  2845. case I915_CACHING_CACHED:
  2846. level = I915_CACHE_LLC;
  2847. break;
  2848. default:
  2849. return -EINVAL;
  2850. }
  2851. ret = i915_mutex_lock_interruptible(dev);
  2852. if (ret)
  2853. return ret;
  2854. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2855. if (&obj->base == NULL) {
  2856. ret = -ENOENT;
  2857. goto unlock;
  2858. }
  2859. ret = i915_gem_object_set_cache_level(obj, level);
  2860. drm_gem_object_unreference(&obj->base);
  2861. unlock:
  2862. mutex_unlock(&dev->struct_mutex);
  2863. return ret;
  2864. }
  2865. /*
  2866. * Prepare buffer for display plane (scanout, cursors, etc).
  2867. * Can be called from an uninterruptible phase (modesetting) and allows
  2868. * any flushes to be pipelined (for pageflips).
  2869. */
  2870. int
  2871. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  2872. u32 alignment,
  2873. struct intel_ring_buffer *pipelined)
  2874. {
  2875. u32 old_read_domains, old_write_domain;
  2876. int ret;
  2877. if (pipelined != obj->ring) {
  2878. ret = i915_gem_object_sync(obj, pipelined);
  2879. if (ret)
  2880. return ret;
  2881. }
  2882. /* The display engine is not coherent with the LLC cache on gen6. As
  2883. * a result, we make sure that the pinning that is about to occur is
  2884. * done with uncached PTEs. This is lowest common denominator for all
  2885. * chipsets.
  2886. *
  2887. * However for gen6+, we could do better by using the GFDT bit instead
  2888. * of uncaching, which would allow us to flush all the LLC-cached data
  2889. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  2890. */
  2891. ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
  2892. if (ret)
  2893. return ret;
  2894. /* As the user may map the buffer once pinned in the display plane
  2895. * (e.g. libkms for the bootup splash), we have to ensure that we
  2896. * always use map_and_fenceable for all scanout buffers.
  2897. */
  2898. ret = i915_gem_object_pin(obj, alignment, true, false);
  2899. if (ret)
  2900. return ret;
  2901. i915_gem_object_flush_cpu_write_domain(obj);
  2902. old_write_domain = obj->base.write_domain;
  2903. old_read_domains = obj->base.read_domains;
  2904. /* It should now be out of any other write domains, and we can update
  2905. * the domain values for our changes.
  2906. */
  2907. obj->base.write_domain = 0;
  2908. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2909. trace_i915_gem_object_change_domain(obj,
  2910. old_read_domains,
  2911. old_write_domain);
  2912. return 0;
  2913. }
  2914. int
  2915. i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
  2916. {
  2917. int ret;
  2918. if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
  2919. return 0;
  2920. ret = i915_gem_object_wait_rendering(obj, false);
  2921. if (ret)
  2922. return ret;
  2923. /* Ensure that we invalidate the GPU's caches and TLBs. */
  2924. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  2925. return 0;
  2926. }
  2927. /**
  2928. * Moves a single object to the CPU read, and possibly write domain.
  2929. *
  2930. * This function returns when the move is complete, including waiting on
  2931. * flushes to occur.
  2932. */
  2933. int
  2934. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  2935. {
  2936. uint32_t old_write_domain, old_read_domains;
  2937. int ret;
  2938. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  2939. return 0;
  2940. ret = i915_gem_object_wait_rendering(obj, !write);
  2941. if (ret)
  2942. return ret;
  2943. i915_gem_object_flush_gtt_write_domain(obj);
  2944. old_write_domain = obj->base.write_domain;
  2945. old_read_domains = obj->base.read_domains;
  2946. /* Flush the CPU cache if it's still invalid. */
  2947. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  2948. i915_gem_clflush_object(obj);
  2949. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  2950. }
  2951. /* It should now be out of any other write domains, and we can update
  2952. * the domain values for our changes.
  2953. */
  2954. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2955. /* If we're writing through the CPU, then the GPU read domains will
  2956. * need to be invalidated at next use.
  2957. */
  2958. if (write) {
  2959. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2960. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2961. }
  2962. trace_i915_gem_object_change_domain(obj,
  2963. old_read_domains,
  2964. old_write_domain);
  2965. return 0;
  2966. }
  2967. /* Throttle our rendering by waiting until the ring has completed our requests
  2968. * emitted over 20 msec ago.
  2969. *
  2970. * Note that if we were to use the current jiffies each time around the loop,
  2971. * we wouldn't escape the function with any frames outstanding if the time to
  2972. * render a frame was over 20ms.
  2973. *
  2974. * This should get us reasonable parallelism between CPU and GPU but also
  2975. * relatively low latency when blocking on a particular request to finish.
  2976. */
  2977. static int
  2978. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  2979. {
  2980. struct drm_i915_private *dev_priv = dev->dev_private;
  2981. struct drm_i915_file_private *file_priv = file->driver_priv;
  2982. unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
  2983. struct drm_i915_gem_request *request;
  2984. struct intel_ring_buffer *ring = NULL;
  2985. unsigned reset_counter;
  2986. u32 seqno = 0;
  2987. int ret;
  2988. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  2989. if (ret)
  2990. return ret;
  2991. ret = i915_gem_check_wedge(&dev_priv->gpu_error, false);
  2992. if (ret)
  2993. return ret;
  2994. spin_lock(&file_priv->mm.lock);
  2995. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  2996. if (time_after_eq(request->emitted_jiffies, recent_enough))
  2997. break;
  2998. ring = request->ring;
  2999. seqno = request->seqno;
  3000. }
  3001. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  3002. spin_unlock(&file_priv->mm.lock);
  3003. if (seqno == 0)
  3004. return 0;
  3005. ret = __wait_seqno(ring, seqno, reset_counter, true, NULL);
  3006. if (ret == 0)
  3007. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  3008. return ret;
  3009. }
  3010. int
  3011. i915_gem_object_pin(struct drm_i915_gem_object *obj,
  3012. uint32_t alignment,
  3013. bool map_and_fenceable,
  3014. bool nonblocking)
  3015. {
  3016. int ret;
  3017. if (WARN_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
  3018. return -EBUSY;
  3019. if (i915_gem_obj_ggtt_bound(obj)) {
  3020. if ((alignment && i915_gem_obj_ggtt_offset(obj) & (alignment - 1)) ||
  3021. (map_and_fenceable && !obj->map_and_fenceable)) {
  3022. WARN(obj->pin_count,
  3023. "bo is already pinned with incorrect alignment:"
  3024. " offset=%lx, req.alignment=%x, req.map_and_fenceable=%d,"
  3025. " obj->map_and_fenceable=%d\n",
  3026. i915_gem_obj_ggtt_offset(obj), alignment,
  3027. map_and_fenceable,
  3028. obj->map_and_fenceable);
  3029. ret = i915_gem_object_unbind(obj);
  3030. if (ret)
  3031. return ret;
  3032. }
  3033. }
  3034. if (!i915_gem_obj_ggtt_bound(obj)) {
  3035. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3036. ret = i915_gem_object_bind_to_gtt(obj, alignment,
  3037. map_and_fenceable,
  3038. nonblocking);
  3039. if (ret)
  3040. return ret;
  3041. if (!dev_priv->mm.aliasing_ppgtt)
  3042. i915_gem_gtt_bind_object(obj, obj->cache_level);
  3043. }
  3044. if (!obj->has_global_gtt_mapping && map_and_fenceable)
  3045. i915_gem_gtt_bind_object(obj, obj->cache_level);
  3046. obj->pin_count++;
  3047. obj->pin_mappable |= map_and_fenceable;
  3048. return 0;
  3049. }
  3050. void
  3051. i915_gem_object_unpin(struct drm_i915_gem_object *obj)
  3052. {
  3053. BUG_ON(obj->pin_count == 0);
  3054. BUG_ON(!i915_gem_obj_ggtt_bound(obj));
  3055. if (--obj->pin_count == 0)
  3056. obj->pin_mappable = false;
  3057. }
  3058. int
  3059. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  3060. struct drm_file *file)
  3061. {
  3062. struct drm_i915_gem_pin *args = data;
  3063. struct drm_i915_gem_object *obj;
  3064. int ret;
  3065. ret = i915_mutex_lock_interruptible(dev);
  3066. if (ret)
  3067. return ret;
  3068. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3069. if (&obj->base == NULL) {
  3070. ret = -ENOENT;
  3071. goto unlock;
  3072. }
  3073. if (obj->madv != I915_MADV_WILLNEED) {
  3074. DRM_ERROR("Attempting to pin a purgeable buffer\n");
  3075. ret = -EINVAL;
  3076. goto out;
  3077. }
  3078. if (obj->pin_filp != NULL && obj->pin_filp != file) {
  3079. DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
  3080. args->handle);
  3081. ret = -EINVAL;
  3082. goto out;
  3083. }
  3084. if (obj->user_pin_count == 0) {
  3085. ret = i915_gem_object_pin(obj, args->alignment, true, false);
  3086. if (ret)
  3087. goto out;
  3088. }
  3089. obj->user_pin_count++;
  3090. obj->pin_filp = file;
  3091. /* XXX - flush the CPU caches for pinned objects
  3092. * as the X server doesn't manage domains yet
  3093. */
  3094. i915_gem_object_flush_cpu_write_domain(obj);
  3095. args->offset = i915_gem_obj_ggtt_offset(obj);
  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_unpin_ioctl(struct drm_device *dev, void *data,
  3104. struct drm_file *file)
  3105. {
  3106. struct drm_i915_gem_pin *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. if (obj->pin_filp != file) {
  3118. DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  3119. args->handle);
  3120. ret = -EINVAL;
  3121. goto out;
  3122. }
  3123. obj->user_pin_count--;
  3124. if (obj->user_pin_count == 0) {
  3125. obj->pin_filp = NULL;
  3126. i915_gem_object_unpin(obj);
  3127. }
  3128. out:
  3129. drm_gem_object_unreference(&obj->base);
  3130. unlock:
  3131. mutex_unlock(&dev->struct_mutex);
  3132. return ret;
  3133. }
  3134. int
  3135. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3136. struct drm_file *file)
  3137. {
  3138. struct drm_i915_gem_busy *args = data;
  3139. struct drm_i915_gem_object *obj;
  3140. int ret;
  3141. ret = i915_mutex_lock_interruptible(dev);
  3142. if (ret)
  3143. return ret;
  3144. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3145. if (&obj->base == NULL) {
  3146. ret = -ENOENT;
  3147. goto unlock;
  3148. }
  3149. /* Count all active objects as busy, even if they are currently not used
  3150. * by the gpu. Users of this interface expect objects to eventually
  3151. * become non-busy without any further actions, therefore emit any
  3152. * necessary flushes here.
  3153. */
  3154. ret = i915_gem_object_flush_active(obj);
  3155. args->busy = obj->active;
  3156. if (obj->ring) {
  3157. BUILD_BUG_ON(I915_NUM_RINGS > 16);
  3158. args->busy |= intel_ring_flag(obj->ring) << 16;
  3159. }
  3160. drm_gem_object_unreference(&obj->base);
  3161. unlock:
  3162. mutex_unlock(&dev->struct_mutex);
  3163. return ret;
  3164. }
  3165. int
  3166. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3167. struct drm_file *file_priv)
  3168. {
  3169. return i915_gem_ring_throttle(dev, file_priv);
  3170. }
  3171. int
  3172. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3173. struct drm_file *file_priv)
  3174. {
  3175. struct drm_i915_gem_madvise *args = data;
  3176. struct drm_i915_gem_object *obj;
  3177. int ret;
  3178. switch (args->madv) {
  3179. case I915_MADV_DONTNEED:
  3180. case I915_MADV_WILLNEED:
  3181. break;
  3182. default:
  3183. return -EINVAL;
  3184. }
  3185. ret = i915_mutex_lock_interruptible(dev);
  3186. if (ret)
  3187. return ret;
  3188. obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
  3189. if (&obj->base == NULL) {
  3190. ret = -ENOENT;
  3191. goto unlock;
  3192. }
  3193. if (obj->pin_count) {
  3194. ret = -EINVAL;
  3195. goto out;
  3196. }
  3197. if (obj->madv != __I915_MADV_PURGED)
  3198. obj->madv = args->madv;
  3199. /* if the object is no longer attached, discard its backing storage */
  3200. if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
  3201. i915_gem_object_truncate(obj);
  3202. args->retained = obj->madv != __I915_MADV_PURGED;
  3203. out:
  3204. drm_gem_object_unreference(&obj->base);
  3205. unlock:
  3206. mutex_unlock(&dev->struct_mutex);
  3207. return ret;
  3208. }
  3209. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3210. const struct drm_i915_gem_object_ops *ops)
  3211. {
  3212. INIT_LIST_HEAD(&obj->mm_list);
  3213. INIT_LIST_HEAD(&obj->global_list);
  3214. INIT_LIST_HEAD(&obj->ring_list);
  3215. INIT_LIST_HEAD(&obj->exec_list);
  3216. INIT_LIST_HEAD(&obj->vma_list);
  3217. obj->ops = ops;
  3218. obj->fence_reg = I915_FENCE_REG_NONE;
  3219. obj->madv = I915_MADV_WILLNEED;
  3220. /* Avoid an unnecessary call to unbind on the first bind. */
  3221. obj->map_and_fenceable = true;
  3222. i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
  3223. }
  3224. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3225. .get_pages = i915_gem_object_get_pages_gtt,
  3226. .put_pages = i915_gem_object_put_pages_gtt,
  3227. };
  3228. struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
  3229. size_t size)
  3230. {
  3231. struct drm_i915_gem_object *obj;
  3232. struct address_space *mapping;
  3233. gfp_t mask;
  3234. obj = i915_gem_object_alloc(dev);
  3235. if (obj == NULL)
  3236. return NULL;
  3237. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  3238. i915_gem_object_free(obj);
  3239. return NULL;
  3240. }
  3241. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3242. if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
  3243. /* 965gm cannot relocate objects above 4GiB. */
  3244. mask &= ~__GFP_HIGHMEM;
  3245. mask |= __GFP_DMA32;
  3246. }
  3247. mapping = file_inode(obj->base.filp)->i_mapping;
  3248. mapping_set_gfp_mask(mapping, mask);
  3249. i915_gem_object_init(obj, &i915_gem_object_ops);
  3250. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3251. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3252. if (HAS_LLC(dev)) {
  3253. /* On some devices, we can have the GPU use the LLC (the CPU
  3254. * cache) for about a 10% performance improvement
  3255. * compared to uncached. Graphics requests other than
  3256. * display scanout are coherent with the CPU in
  3257. * accessing this cache. This means in this mode we
  3258. * don't need to clflush on the CPU side, and on the
  3259. * GPU side we only need to flush internal caches to
  3260. * get data visible to the CPU.
  3261. *
  3262. * However, we maintain the display planes as UC, and so
  3263. * need to rebind when first used as such.
  3264. */
  3265. obj->cache_level = I915_CACHE_LLC;
  3266. } else
  3267. obj->cache_level = I915_CACHE_NONE;
  3268. trace_i915_gem_object_create(obj);
  3269. return obj;
  3270. }
  3271. int i915_gem_init_object(struct drm_gem_object *obj)
  3272. {
  3273. BUG();
  3274. return 0;
  3275. }
  3276. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3277. {
  3278. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3279. struct drm_device *dev = obj->base.dev;
  3280. drm_i915_private_t *dev_priv = dev->dev_private;
  3281. trace_i915_gem_object_destroy(obj);
  3282. if (obj->phys_obj)
  3283. i915_gem_detach_phys_object(dev, obj);
  3284. obj->pin_count = 0;
  3285. if (WARN_ON(i915_gem_object_unbind(obj) == -ERESTARTSYS)) {
  3286. bool was_interruptible;
  3287. was_interruptible = dev_priv->mm.interruptible;
  3288. dev_priv->mm.interruptible = false;
  3289. WARN_ON(i915_gem_object_unbind(obj));
  3290. dev_priv->mm.interruptible = was_interruptible;
  3291. }
  3292. /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
  3293. * before progressing. */
  3294. if (obj->stolen)
  3295. i915_gem_object_unpin_pages(obj);
  3296. if (WARN_ON(obj->pages_pin_count))
  3297. obj->pages_pin_count = 0;
  3298. i915_gem_object_put_pages(obj);
  3299. i915_gem_object_free_mmap_offset(obj);
  3300. i915_gem_object_release_stolen(obj);
  3301. BUG_ON(obj->pages);
  3302. if (obj->base.import_attach)
  3303. drm_prime_gem_destroy(&obj->base, NULL);
  3304. drm_gem_object_release(&obj->base);
  3305. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  3306. kfree(obj->bit_17);
  3307. i915_gem_object_free(obj);
  3308. }
  3309. struct i915_vma *i915_gem_vma_create(struct drm_i915_gem_object *obj,
  3310. struct i915_address_space *vm)
  3311. {
  3312. struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL);
  3313. if (vma == NULL)
  3314. return ERR_PTR(-ENOMEM);
  3315. INIT_LIST_HEAD(&vma->vma_link);
  3316. vma->vm = vm;
  3317. vma->obj = obj;
  3318. return vma;
  3319. }
  3320. void i915_gem_vma_destroy(struct i915_vma *vma)
  3321. {
  3322. WARN_ON(vma->node.allocated);
  3323. kfree(vma);
  3324. }
  3325. int
  3326. i915_gem_idle(struct drm_device *dev)
  3327. {
  3328. drm_i915_private_t *dev_priv = dev->dev_private;
  3329. int ret;
  3330. if (dev_priv->ums.mm_suspended) {
  3331. mutex_unlock(&dev->struct_mutex);
  3332. return 0;
  3333. }
  3334. ret = i915_gpu_idle(dev);
  3335. if (ret) {
  3336. mutex_unlock(&dev->struct_mutex);
  3337. return ret;
  3338. }
  3339. i915_gem_retire_requests(dev);
  3340. /* Under UMS, be paranoid and evict. */
  3341. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3342. i915_gem_evict_everything(dev);
  3343. del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
  3344. i915_kernel_lost_context(dev);
  3345. i915_gem_cleanup_ringbuffer(dev);
  3346. /* Cancel the retire work handler, which should be idle now. */
  3347. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  3348. return 0;
  3349. }
  3350. void i915_gem_l3_remap(struct drm_device *dev)
  3351. {
  3352. drm_i915_private_t *dev_priv = dev->dev_private;
  3353. u32 misccpctl;
  3354. int i;
  3355. if (!HAS_L3_GPU_CACHE(dev))
  3356. return;
  3357. if (!dev_priv->l3_parity.remap_info)
  3358. return;
  3359. misccpctl = I915_READ(GEN7_MISCCPCTL);
  3360. I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
  3361. POSTING_READ(GEN7_MISCCPCTL);
  3362. for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
  3363. u32 remap = I915_READ(GEN7_L3LOG_BASE + i);
  3364. if (remap && remap != dev_priv->l3_parity.remap_info[i/4])
  3365. DRM_DEBUG("0x%x was already programmed to %x\n",
  3366. GEN7_L3LOG_BASE + i, remap);
  3367. if (remap && !dev_priv->l3_parity.remap_info[i/4])
  3368. DRM_DEBUG_DRIVER("Clearing remapped register\n");
  3369. I915_WRITE(GEN7_L3LOG_BASE + i, dev_priv->l3_parity.remap_info[i/4]);
  3370. }
  3371. /* Make sure all the writes land before disabling dop clock gating */
  3372. POSTING_READ(GEN7_L3LOG_BASE);
  3373. I915_WRITE(GEN7_MISCCPCTL, misccpctl);
  3374. }
  3375. void i915_gem_init_swizzling(struct drm_device *dev)
  3376. {
  3377. drm_i915_private_t *dev_priv = dev->dev_private;
  3378. if (INTEL_INFO(dev)->gen < 5 ||
  3379. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  3380. return;
  3381. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  3382. DISP_TILE_SURFACE_SWIZZLING);
  3383. if (IS_GEN5(dev))
  3384. return;
  3385. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  3386. if (IS_GEN6(dev))
  3387. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  3388. else if (IS_GEN7(dev))
  3389. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  3390. else
  3391. BUG();
  3392. }
  3393. static bool
  3394. intel_enable_blt(struct drm_device *dev)
  3395. {
  3396. if (!HAS_BLT(dev))
  3397. return false;
  3398. /* The blitter was dysfunctional on early prototypes */
  3399. if (IS_GEN6(dev) && dev->pdev->revision < 8) {
  3400. DRM_INFO("BLT not supported on this pre-production hardware;"
  3401. " graphics performance will be degraded.\n");
  3402. return false;
  3403. }
  3404. return true;
  3405. }
  3406. static int i915_gem_init_rings(struct drm_device *dev)
  3407. {
  3408. struct drm_i915_private *dev_priv = dev->dev_private;
  3409. int ret;
  3410. ret = intel_init_render_ring_buffer(dev);
  3411. if (ret)
  3412. return ret;
  3413. if (HAS_BSD(dev)) {
  3414. ret = intel_init_bsd_ring_buffer(dev);
  3415. if (ret)
  3416. goto cleanup_render_ring;
  3417. }
  3418. if (intel_enable_blt(dev)) {
  3419. ret = intel_init_blt_ring_buffer(dev);
  3420. if (ret)
  3421. goto cleanup_bsd_ring;
  3422. }
  3423. if (HAS_VEBOX(dev)) {
  3424. ret = intel_init_vebox_ring_buffer(dev);
  3425. if (ret)
  3426. goto cleanup_blt_ring;
  3427. }
  3428. ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
  3429. if (ret)
  3430. goto cleanup_vebox_ring;
  3431. return 0;
  3432. cleanup_vebox_ring:
  3433. intel_cleanup_ring_buffer(&dev_priv->ring[VECS]);
  3434. cleanup_blt_ring:
  3435. intel_cleanup_ring_buffer(&dev_priv->ring[BCS]);
  3436. cleanup_bsd_ring:
  3437. intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
  3438. cleanup_render_ring:
  3439. intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
  3440. return ret;
  3441. }
  3442. int
  3443. i915_gem_init_hw(struct drm_device *dev)
  3444. {
  3445. drm_i915_private_t *dev_priv = dev->dev_private;
  3446. int ret;
  3447. if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
  3448. return -EIO;
  3449. if (dev_priv->ellc_size)
  3450. I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  3451. if (HAS_PCH_NOP(dev)) {
  3452. u32 temp = I915_READ(GEN7_MSG_CTL);
  3453. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  3454. I915_WRITE(GEN7_MSG_CTL, temp);
  3455. }
  3456. i915_gem_l3_remap(dev);
  3457. i915_gem_init_swizzling(dev);
  3458. ret = i915_gem_init_rings(dev);
  3459. if (ret)
  3460. return ret;
  3461. /*
  3462. * XXX: There was some w/a described somewhere suggesting loading
  3463. * contexts before PPGTT.
  3464. */
  3465. i915_gem_context_init(dev);
  3466. if (dev_priv->mm.aliasing_ppgtt) {
  3467. ret = dev_priv->mm.aliasing_ppgtt->enable(dev);
  3468. if (ret) {
  3469. i915_gem_cleanup_aliasing_ppgtt(dev);
  3470. DRM_INFO("PPGTT enable failed. This is not fatal, but unexpected\n");
  3471. }
  3472. }
  3473. return 0;
  3474. }
  3475. int i915_gem_init(struct drm_device *dev)
  3476. {
  3477. struct drm_i915_private *dev_priv = dev->dev_private;
  3478. int ret;
  3479. mutex_lock(&dev->struct_mutex);
  3480. if (IS_VALLEYVIEW(dev)) {
  3481. /* VLVA0 (potential hack), BIOS isn't actually waking us */
  3482. I915_WRITE(VLV_GTLC_WAKE_CTRL, 1);
  3483. if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) & 1) == 1, 10))
  3484. DRM_DEBUG_DRIVER("allow wake ack timed out\n");
  3485. }
  3486. i915_gem_init_global_gtt(dev);
  3487. ret = i915_gem_init_hw(dev);
  3488. mutex_unlock(&dev->struct_mutex);
  3489. if (ret) {
  3490. i915_gem_cleanup_aliasing_ppgtt(dev);
  3491. return ret;
  3492. }
  3493. /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */
  3494. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3495. dev_priv->dri1.allow_batchbuffer = 1;
  3496. return 0;
  3497. }
  3498. void
  3499. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  3500. {
  3501. drm_i915_private_t *dev_priv = dev->dev_private;
  3502. struct intel_ring_buffer *ring;
  3503. int i;
  3504. for_each_ring(ring, dev_priv, i)
  3505. intel_cleanup_ring_buffer(ring);
  3506. }
  3507. int
  3508. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  3509. struct drm_file *file_priv)
  3510. {
  3511. struct drm_i915_private *dev_priv = dev->dev_private;
  3512. int ret;
  3513. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3514. return 0;
  3515. if (i915_reset_in_progress(&dev_priv->gpu_error)) {
  3516. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  3517. atomic_set(&dev_priv->gpu_error.reset_counter, 0);
  3518. }
  3519. mutex_lock(&dev->struct_mutex);
  3520. dev_priv->ums.mm_suspended = 0;
  3521. ret = i915_gem_init_hw(dev);
  3522. if (ret != 0) {
  3523. mutex_unlock(&dev->struct_mutex);
  3524. return ret;
  3525. }
  3526. BUG_ON(!list_empty(&dev_priv->gtt.base.active_list));
  3527. mutex_unlock(&dev->struct_mutex);
  3528. ret = drm_irq_install(dev);
  3529. if (ret)
  3530. goto cleanup_ringbuffer;
  3531. return 0;
  3532. cleanup_ringbuffer:
  3533. mutex_lock(&dev->struct_mutex);
  3534. i915_gem_cleanup_ringbuffer(dev);
  3535. dev_priv->ums.mm_suspended = 1;
  3536. mutex_unlock(&dev->struct_mutex);
  3537. return ret;
  3538. }
  3539. int
  3540. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  3541. struct drm_file *file_priv)
  3542. {
  3543. struct drm_i915_private *dev_priv = dev->dev_private;
  3544. int ret;
  3545. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3546. return 0;
  3547. drm_irq_uninstall(dev);
  3548. mutex_lock(&dev->struct_mutex);
  3549. ret = i915_gem_idle(dev);
  3550. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  3551. * We need to replace this with a semaphore, or something.
  3552. * And not confound ums.mm_suspended!
  3553. */
  3554. if (ret != 0)
  3555. dev_priv->ums.mm_suspended = 1;
  3556. mutex_unlock(&dev->struct_mutex);
  3557. return ret;
  3558. }
  3559. void
  3560. i915_gem_lastclose(struct drm_device *dev)
  3561. {
  3562. int ret;
  3563. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3564. return;
  3565. mutex_lock(&dev->struct_mutex);
  3566. ret = i915_gem_idle(dev);
  3567. if (ret)
  3568. DRM_ERROR("failed to idle hardware: %d\n", ret);
  3569. mutex_unlock(&dev->struct_mutex);
  3570. }
  3571. static void
  3572. init_ring_lists(struct intel_ring_buffer *ring)
  3573. {
  3574. INIT_LIST_HEAD(&ring->active_list);
  3575. INIT_LIST_HEAD(&ring->request_list);
  3576. }
  3577. void
  3578. i915_gem_load(struct drm_device *dev)
  3579. {
  3580. drm_i915_private_t *dev_priv = dev->dev_private;
  3581. int i;
  3582. dev_priv->slab =
  3583. kmem_cache_create("i915_gem_object",
  3584. sizeof(struct drm_i915_gem_object), 0,
  3585. SLAB_HWCACHE_ALIGN,
  3586. NULL);
  3587. INIT_LIST_HEAD(&dev_priv->gtt.base.active_list);
  3588. INIT_LIST_HEAD(&dev_priv->gtt.base.inactive_list);
  3589. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  3590. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  3591. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  3592. for (i = 0; i < I915_NUM_RINGS; i++)
  3593. init_ring_lists(&dev_priv->ring[i]);
  3594. for (i = 0; i < I915_MAX_NUM_FENCES; i++)
  3595. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  3596. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  3597. i915_gem_retire_work_handler);
  3598. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  3599. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  3600. if (IS_GEN3(dev)) {
  3601. I915_WRITE(MI_ARB_STATE,
  3602. _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
  3603. }
  3604. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  3605. /* Old X drivers will take 0-2 for front, back, depth buffers */
  3606. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3607. dev_priv->fence_reg_start = 3;
  3608. if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev))
  3609. dev_priv->num_fence_regs = 32;
  3610. else if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3611. dev_priv->num_fence_regs = 16;
  3612. else
  3613. dev_priv->num_fence_regs = 8;
  3614. /* Initialize fence registers to zero */
  3615. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  3616. i915_gem_restore_fences(dev);
  3617. i915_gem_detect_bit_6_swizzle(dev);
  3618. init_waitqueue_head(&dev_priv->pending_flip_queue);
  3619. dev_priv->mm.interruptible = true;
  3620. dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
  3621. dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
  3622. register_shrinker(&dev_priv->mm.inactive_shrinker);
  3623. }
  3624. /*
  3625. * Create a physically contiguous memory object for this object
  3626. * e.g. for cursor + overlay regs
  3627. */
  3628. static int i915_gem_init_phys_object(struct drm_device *dev,
  3629. int id, int size, int align)
  3630. {
  3631. drm_i915_private_t *dev_priv = dev->dev_private;
  3632. struct drm_i915_gem_phys_object *phys_obj;
  3633. int ret;
  3634. if (dev_priv->mm.phys_objs[id - 1] || !size)
  3635. return 0;
  3636. phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
  3637. if (!phys_obj)
  3638. return -ENOMEM;
  3639. phys_obj->id = id;
  3640. phys_obj->handle = drm_pci_alloc(dev, size, align);
  3641. if (!phys_obj->handle) {
  3642. ret = -ENOMEM;
  3643. goto kfree_obj;
  3644. }
  3645. #ifdef CONFIG_X86
  3646. set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3647. #endif
  3648. dev_priv->mm.phys_objs[id - 1] = phys_obj;
  3649. return 0;
  3650. kfree_obj:
  3651. kfree(phys_obj);
  3652. return ret;
  3653. }
  3654. static void i915_gem_free_phys_object(struct drm_device *dev, int id)
  3655. {
  3656. drm_i915_private_t *dev_priv = dev->dev_private;
  3657. struct drm_i915_gem_phys_object *phys_obj;
  3658. if (!dev_priv->mm.phys_objs[id - 1])
  3659. return;
  3660. phys_obj = dev_priv->mm.phys_objs[id - 1];
  3661. if (phys_obj->cur_obj) {
  3662. i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
  3663. }
  3664. #ifdef CONFIG_X86
  3665. set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3666. #endif
  3667. drm_pci_free(dev, phys_obj->handle);
  3668. kfree(phys_obj);
  3669. dev_priv->mm.phys_objs[id - 1] = NULL;
  3670. }
  3671. void i915_gem_free_all_phys_object(struct drm_device *dev)
  3672. {
  3673. int i;
  3674. for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
  3675. i915_gem_free_phys_object(dev, i);
  3676. }
  3677. void i915_gem_detach_phys_object(struct drm_device *dev,
  3678. struct drm_i915_gem_object *obj)
  3679. {
  3680. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  3681. char *vaddr;
  3682. int i;
  3683. int page_count;
  3684. if (!obj->phys_obj)
  3685. return;
  3686. vaddr = obj->phys_obj->handle->vaddr;
  3687. page_count = obj->base.size / PAGE_SIZE;
  3688. for (i = 0; i < page_count; i++) {
  3689. struct page *page = shmem_read_mapping_page(mapping, i);
  3690. if (!IS_ERR(page)) {
  3691. char *dst = kmap_atomic(page);
  3692. memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
  3693. kunmap_atomic(dst);
  3694. drm_clflush_pages(&page, 1);
  3695. set_page_dirty(page);
  3696. mark_page_accessed(page);
  3697. page_cache_release(page);
  3698. }
  3699. }
  3700. i915_gem_chipset_flush(dev);
  3701. obj->phys_obj->cur_obj = NULL;
  3702. obj->phys_obj = NULL;
  3703. }
  3704. int
  3705. i915_gem_attach_phys_object(struct drm_device *dev,
  3706. struct drm_i915_gem_object *obj,
  3707. int id,
  3708. int align)
  3709. {
  3710. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  3711. drm_i915_private_t *dev_priv = dev->dev_private;
  3712. int ret = 0;
  3713. int page_count;
  3714. int i;
  3715. if (id > I915_MAX_PHYS_OBJECT)
  3716. return -EINVAL;
  3717. if (obj->phys_obj) {
  3718. if (obj->phys_obj->id == id)
  3719. return 0;
  3720. i915_gem_detach_phys_object(dev, obj);
  3721. }
  3722. /* create a new object */
  3723. if (!dev_priv->mm.phys_objs[id - 1]) {
  3724. ret = i915_gem_init_phys_object(dev, id,
  3725. obj->base.size, align);
  3726. if (ret) {
  3727. DRM_ERROR("failed to init phys object %d size: %zu\n",
  3728. id, obj->base.size);
  3729. return ret;
  3730. }
  3731. }
  3732. /* bind to the object */
  3733. obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
  3734. obj->phys_obj->cur_obj = obj;
  3735. page_count = obj->base.size / PAGE_SIZE;
  3736. for (i = 0; i < page_count; i++) {
  3737. struct page *page;
  3738. char *dst, *src;
  3739. page = shmem_read_mapping_page(mapping, i);
  3740. if (IS_ERR(page))
  3741. return PTR_ERR(page);
  3742. src = kmap_atomic(page);
  3743. dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  3744. memcpy(dst, src, PAGE_SIZE);
  3745. kunmap_atomic(src);
  3746. mark_page_accessed(page);
  3747. page_cache_release(page);
  3748. }
  3749. return 0;
  3750. }
  3751. static int
  3752. i915_gem_phys_pwrite(struct drm_device *dev,
  3753. struct drm_i915_gem_object *obj,
  3754. struct drm_i915_gem_pwrite *args,
  3755. struct drm_file *file_priv)
  3756. {
  3757. void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
  3758. char __user *user_data = to_user_ptr(args->data_ptr);
  3759. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  3760. unsigned long unwritten;
  3761. /* The physical object once assigned is fixed for the lifetime
  3762. * of the obj, so we can safely drop the lock and continue
  3763. * to access vaddr.
  3764. */
  3765. mutex_unlock(&dev->struct_mutex);
  3766. unwritten = copy_from_user(vaddr, user_data, args->size);
  3767. mutex_lock(&dev->struct_mutex);
  3768. if (unwritten)
  3769. return -EFAULT;
  3770. }
  3771. i915_gem_chipset_flush(dev);
  3772. return 0;
  3773. }
  3774. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  3775. {
  3776. struct drm_i915_file_private *file_priv = file->driver_priv;
  3777. /* Clean up our request list when the client is going away, so that
  3778. * later retire_requests won't dereference our soon-to-be-gone
  3779. * file_priv.
  3780. */
  3781. spin_lock(&file_priv->mm.lock);
  3782. while (!list_empty(&file_priv->mm.request_list)) {
  3783. struct drm_i915_gem_request *request;
  3784. request = list_first_entry(&file_priv->mm.request_list,
  3785. struct drm_i915_gem_request,
  3786. client_list);
  3787. list_del(&request->client_list);
  3788. request->file_priv = NULL;
  3789. }
  3790. spin_unlock(&file_priv->mm.lock);
  3791. }
  3792. static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
  3793. {
  3794. if (!mutex_is_locked(mutex))
  3795. return false;
  3796. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)
  3797. return mutex->owner == task;
  3798. #else
  3799. /* Since UP may be pre-empted, we cannot assume that we own the lock */
  3800. return false;
  3801. #endif
  3802. }
  3803. static int
  3804. i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
  3805. {
  3806. struct drm_i915_private *dev_priv =
  3807. container_of(shrinker,
  3808. struct drm_i915_private,
  3809. mm.inactive_shrinker);
  3810. struct drm_device *dev = dev_priv->dev;
  3811. struct i915_address_space *vm = &dev_priv->gtt.base;
  3812. struct drm_i915_gem_object *obj;
  3813. int nr_to_scan = sc->nr_to_scan;
  3814. bool unlock = true;
  3815. int cnt;
  3816. if (!mutex_trylock(&dev->struct_mutex)) {
  3817. if (!mutex_is_locked_by(&dev->struct_mutex, current))
  3818. return 0;
  3819. if (dev_priv->mm.shrinker_no_lock_stealing)
  3820. return 0;
  3821. unlock = false;
  3822. }
  3823. if (nr_to_scan) {
  3824. nr_to_scan -= i915_gem_purge(dev_priv, nr_to_scan);
  3825. if (nr_to_scan > 0)
  3826. nr_to_scan -= __i915_gem_shrink(dev_priv, nr_to_scan,
  3827. false);
  3828. if (nr_to_scan > 0)
  3829. i915_gem_shrink_all(dev_priv);
  3830. }
  3831. cnt = 0;
  3832. list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list)
  3833. if (obj->pages_pin_count == 0)
  3834. cnt += obj->base.size >> PAGE_SHIFT;
  3835. list_for_each_entry(obj, &vm->inactive_list, mm_list)
  3836. if (obj->pin_count == 0 && obj->pages_pin_count == 0)
  3837. cnt += obj->base.size >> PAGE_SHIFT;
  3838. if (unlock)
  3839. mutex_unlock(&dev->struct_mutex);
  3840. return cnt;
  3841. }